By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

synchronized statements

P: n/a
Hi,

I am creating a new thread for each of the connections to the server:

public class Node_C
{
....
while (listening) {
Socket client_socket = server_socket.accept();
Node_CThread node = new Node_CThread(client_socket);
node.start();
}
}

class Node_CThread extends Thread
{
private Socket socket = null;

public Node_CThrad(Socket socket) {
super("Node_CThread");
this.socket = socket;
}

public void run() {
...
System.out.println("IP address = "...);
System.out.println(" IPcount = "...);
System.out.println(" delay = "...);
}
}

Here is what I need to do:
- I would like to print all the System.out.println() statements that are
related to each other (as shown above) one right after the other for
the same thread. That is, I want the System.out.println() statements
to be printed together for the same thread they belong to.
But, since I am using threads, each line of of each thread is executed
at random intervals. Thus I don't know which line of output belongs
to which thread. Btw, I have already labeled each line of output with
the corresponding thread label, but the output is so messy.
- How could I do that?

I have tried to use synchronized(this), as well as
synchronized(System.out) without any luck. That is, the following code
did not work:
public void run() {
...
synchronized(System.out) {
System.out.println("IP address = "...);
System.out.println(" IPcount = "...);
System.out.println(" delay = "...);
}
}

Any help would be greated appreciated.
Thanks in advance...
-John

Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
John Thorner wrote:
- I would like to print all the System.out.println() statements that are
related to each other (as shown above) one right after the other for
the same thread. That is, I want the System.out.println() statements
to be printed together for the same thread they belong to.


A quick solution not involving the synchronized keyword is to print only one
string per thread, i.e. concatenate the strings.

--
Jonas Kongslund
Jul 17 '05 #2

P: n/a
Jonas Kongslund <do**@mail.me.at.all> wrote in message news:<Xi*****************@news.get2net.dk>...
John Thorner wrote:
- I would like to print all the System.out.println() statements that are
related to each other (as shown above) one right after the other for
the same thread. That is, I want the System.out.println() statements
to be printed together for the same thread they belong to.


A quick solution not involving the synchronized keyword is to print only one
string per thread, i.e. concatenate the strings.

This doesn't stop the printing of one set of strings interrupting the
printing of another set - I don't think System.out has any kind of
locking mechanism to ensure two overlapping calls to println cannot
interleave their output on the console. (Unless someone knows better?)

I'm surprised acquiring the lock on System.out didn't work. At first
glance I'd expect that to do the job, as all threads which entered that
synchronized block would be required to own the lock on System.out .
If this genuinely doesn't work (perhaps there's something lurking within
System.out.println which releases the lock?) then the alternative is to
replicate the behaviour with your own object. Just create a singleton
object (a static member will do) and lock on that instead.
-FISH- ><>
Jul 17 '05 #3

P: n/a
FISH wrote:
This doesn't stop the printing of one set of strings interrupting the
printing of another set - I don't think System.out has any kind of
locking mechanism to ensure two overlapping calls to println cannot
interleave their output on the console. (Unless someone knows better?)


Well, it has. See the source for java.io.PrintStream.

--
Jonas Kongslund
Jul 17 '05 #4

P: n/a
I'm only a student at uni but here is my contrib to this discussion. When
you are synchonizing on a thread what you are doing is just synchonizing
threads of the same instance. Core Web Programming by Marty Hall second
edition pg 710 suggests that to synchonize multiple instances of a class you
need to synchonize on a class object or some Arbitary object. Make sure
that it is the same object across all instances of the class. As a straight
quote from Marty Hall...
public class SomeThreadedClass extends Thread{
private static Object lockObject = new object();

public void doSomeOperation(){
synchronized(lockObject){
accessSomeSharedObject();
}
}
}
Noel
"John Thorner" <ze*****@yahoo.com> wrote in message
news:%KXpb.4711$j_4.1271@lakeread05...
Hi,

I am creating a new thread for each of the connections to the server:

public class Node_C
{
...
while (listening) {
Socket client_socket = server_socket.accept();
Node_CThread node = new Node_CThread(client_socket);
node.start();
}
}

class Node_CThread extends Thread
{
private Socket socket = null;

public Node_CThrad(Socket socket) {
super("Node_CThread");
this.socket = socket;
}

public void run() {
...
System.out.println("IP address = "...);
System.out.println(" IPcount = "...);
System.out.println(" delay = "...);
}
}

Here is what I need to do:
- I would like to print all the System.out.println() statements that are
related to each other (as shown above) one right after the other for
the same thread. That is, I want the System.out.println() statements
to be printed together for the same thread they belong to.
But, since I am using threads, each line of of each thread is executed
at random intervals. Thus I don't know which line of output belongs
to which thread. Btw, I have already labeled each line of output with
the corresponding thread label, but the output is so messy.
- How could I do that?

I have tried to use synchronized(this), as well as
synchronized(System.out) without any luck. That is, the following code
did not work:
public void run() {
...
synchronized(System.out) {
System.out.println("IP address = "...);
System.out.println(" IPcount = "...);
System.out.println(" delay = "...);
}
}

Any help would be greated appreciated.
Thanks in advance...
-John

Jul 17 '05 #5

P: n/a
Jonas Kongslund <do**@mail.me.at.all> wrote in message news:<%u*******************@news.get2net.dk>...
FISH wrote:
This doesn't stop the printing of one set of strings interrupting the
printing of another set - I don't think System.out has any kind of
locking mechanism to ensure two overlapping calls to println cannot
interleave their output on the console. (Unless someone knows better?)


Well, it has. See the source for java.io.PrintStream.

Could have sworn I've seen System.out interleave console output - but
obviously I was mistaken. Thanks. ;-)

Anyway, IMHO the best solution is solve the locking mechanism issue, not
concat'ing a load of strings together, prior to output.

-FISH- ><>
Jul 17 '05 #6

P: n/a
FISH wrote:
Anyway, IMHO the best solution is solve the locking mechanism issue, not
concat'ing a load of strings together, prior to output.


IMHO I think both solutions have their advantages and disadvantages. Which
one you want to use really depends on the problem at hand.

--
Jonas Kongslund
Jul 17 '05 #7

P: n/a
FISH wrote:
Jonas Kongslund <do**@mail.me.at.all> wrote in message news:<%u*******************@news.get2net.dk>...
FISH wrote:
This doesn't stop the printing of one set of strings interrupting the
printing of another set - I don't think System.out has any kind of
locking mechanism to ensure two overlapping calls to println cannot
interleave their output on the console. (Unless someone knows better?)


Well, it has. See the source for java.io.PrintStream.


Could have sworn I've seen System.out interleave console output - but
obviously I was mistaken. Thanks. ;-)

What you probably saw was interleaved output from System.out and System.err.

Ray

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.