The Artima Developer Community
Sponsored Link

Java Answers Forum
Synchronize method in threading environment

4 replies on 1 page. Most recent reply: Feb 26, 2002 1:01 AM by Francisco Morales

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic    
Flat View: This topic has 4 replies on 1 page
Steve Que

Posts: 2
Nickname: steveque
Registered: Feb, 2002

Synchronize method in threading environment Posted: Feb 24, 2002 10:01 PM
Reply to this message Reply
Advertisement
Hi,

I've programmed a servlet that would call a synchroized method in a threading environment.

In servlet,
public void doGet(...)
{
SMAThreadPost tp = new SMAThreadPost(session,
context, input_msg, userip);
tp.start();
...
}

In threading class,
public synchronized boolean doWrite()
{
while(...) {
<write data into log file>
}
...
}

public void run()
{
try
{
boolean status = doWrite();
sleep(1);
}
catch(InterruptedException e){}
}

However, I found that the data is recorded interactively in log file. For example 1st thread (Data A) and 2nd thread (Data B):
Data A
Data B
Data A
Data B
Data A
Data B

It is supposed that the 1st thread will lock the synchronized method. However, I guess it doesn't work in this case. Is there anything I missed? Pls noted that both thread are running in the same environment.

Thanks for your kind attention.

Steve :)


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Synchronize method in threading environment Posted: Feb 25, 2002 12:13 AM
Reply to this message Reply
This looks like the correct result. What synchronization prevents is the method running simultaneously in two different threads on the same object. For instance, if your log output had multiple lines and was not synchronized, you might see lines of one log output for A mixed with one from B. Then, when you added the synchronized keyword, that problem would go away.

What is the result you seek, by the way? If you want all log entries for A to lock out entries from B, then you need to make your own mechanism, with maybe an openLog() method that will then let only one thread log to it until it is closed, or something like that.

Steve Que

Posts: 2
Nickname: steveque
Registered: Feb, 2002

Re: Synchronize method in threading environment Posted: Feb 25, 2002 2:00 AM
Reply to this message Reply
Thanks for your kindly help.

However, I want to pinpoint that all "data A"s are recorded within 1st thread and all "data B"s are recorded within 2nd thread.

If 1st thread successfully called the synchronized method "doWrite()" and locked the object. 2nd thread should not be able to write "data B" into the log file until 1st thread finished. Hence "data B" should not be mixed with "data A" in the log file. They should recorded after all "data A"s.

Thanks again.

Steve :)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Synchronize method in threading environment Posted: Feb 25, 2002 8:54 PM
Reply to this message Reply
Well, as I said before, you may want to have an open()/close() or lock()/unlock() type of mechanism for your log, otherwise, how does it know when thread A is finished and it can start logging B's input? If you don't mind them mixing, but want to know what is coming from each thread, then include the thread object in the log (so the log may say "[date][time] message from thread [name]: blah, blah, blah."). You would use Thread.currentThread() and Thread.getName().

Francisco Morales

Posts: 1
Nickname: fmorales
Registered: Feb, 2002

Re: Synchronize method in threading environment Posted: Feb 26, 2002 1:01 AM
Reply to this message Reply
It looks like you want to apply the readers/writes pattern (Grant - Patterns in Java, Vol 2 a.o., I think), that is you allow read operations, but for writing, you want to wait for all working threads to finish and then execute a write operation. It can be implemented through synchronized blocks, and it seems to me, like bv said, that the result is correct.
Hope this help.
regards

Flat View: This topic has 4 replies on 1 page
Topic: sorting arrays Previous Topic    

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use