wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads question." Watch "Threads question." New topic
Author

Threads question.

Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30
Hi folks,

can I get your views on this.

"Given the scenario: This class is intended to allow users to write a series of messages, so that each message is identified with a timestamp and the name of the thread that wrote the message

public class Logger
{
private StringBuilder contents = new StringBuilder();

public void log(String message)
{
contents.append(System.currentTimeMillis());
contents.append(": ");
contents.append(Thread.currentThread().getName());
contents.append("message");
contents.append("\n");
}

public String getContents()
{
return contents.toString();
}
}

How can we ensure that instances of this class can be safely used by multiple threads?"

Apparently, the right answer is "Synchronize both log() and getContents()".

In my opinion, this would still not be enough, as one thread could still be pulling out other thread's message, when it called the getContent() method. Am I wrong?
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Hi,

Once getContents() is synchronized, the thread that acquires the lock on that method, is the same that acquires the lock on log() method. Therefore, any other thread can't enter both methods. Remember this, when a thread acquires a lock, it acquires the lock on all the synchronized methods

Hope this will help
[ May 19, 2008: Message edited by: Mamadou Tour� ]

SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30
Thanks Mamadou, but could it not happen that the thread looses the lock to another thread, between writing the message and eventually invoking the getContent()?

e.g. in UtilityClass
Logger logger = new Logger();

in MyThread..
// Get logger instance
Logger myLogger = utilityClass.getLogger();

// doSomething else, release lock on 'logger' instance
// other thread gets the Logger from utility class, and invokes the log() method on it

// MyThread now calls getContent(), getting the other thread's message
myLogger.getContent()

?

Thanks!
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Once we enter the first synchronized non-static method, we acquire the lock associated the current instance, therefore all the synchronized method are locked an no other thread can acquire the lock.
Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30
I understand that. But what if the thread calls wait() on an object it depends on, between calling the first and the second method? Will it not give up on its locks at that stage? That would then leads to the situation I described above. Please, correct me if I am wrong, thanks!
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Off course when a wait is called, the thread releases its lock, and another thread could acquire it, but in the case given by the K&B exercise, there was no wait called
Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30
Sure, but the thread code was not presented at all either. And the question was "How can we ensure that innstances of this class will be safely used by multiple threads". So, my answer would be - This class, as it is, cannot be made thread-safe, unless we make the call to log() and getContents() from within a synchronized block in the thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads question.