473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

synchronized statements

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
7 8899
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank | last post by:
Hi, In the javadocs regarding many of the java.util classes, it states that the classes are not synchronized, and suggest using the Collections.synchronizedX(...) methods for getting...
5
by: Max Ischenko | last post by:
Hi, I wrote simple implementation of the "synchronized" methods (a-la Java), could you please check if it is OK: def synchronized(method): """ Guards method execution, similar to Java's...
4
by: Rich Sienkiewicz | last post by:
Some classes, like Queue and SortedList, have a Synchronized method which gives a thread safe wrapper object for these classes. But the lock() statement does the same thing. Is there any rules as to...
4
by: chrisben | last post by:
Hi I often use Queue.Synchronized method to create a queue for multithread writing. I also know I could use SyncRoot and lock to write Queue. Could anyone here please explain to me the pros and...
6
by: rmunson8 | last post by:
I have a derived class from the Queue base class. I need it to be thread-safe, so I am using the Synchronized method (among other things out of scope of this issue). The code below compiles, but...
8
by: ASP.Net programmer | last post by:
Hi, I have a few methods in a class that I want to synchronize (make sure they can't be used at the same time by multiple threads). As a Java programmer I just do this: public synchronized...
2
by: yaron | last post by:
Hi, Does MethodImpl Synchronized attribute is like lock(this) ? i mean that a call to Monitor.PulseAll(this) from a Synchronized method will trigger other thread that call to Monitor.Wait(this)...
3
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also...
10
by: master | last post by:
As far as I understand, 'lock' is the only option, isn't it? Namely, If I want to declare a 'synchronised' method, I do it this way: class A { void AMethod() { lock (this) { // the method...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.