Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SCJP practice question  RSS feed

 
sid wagle
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: Interactive Test Engine-310-0552.29

public class TestThread extends Thread {
private static int x;
public synchronized void doThings()
{
int current = x;
current++;
x = current;
}
public void run()
{
doThings();
}
}

Answer to the question which statement is true is:

Declaring the method doThings() as static would make the class thread safe.

Could any one shed some light on why this would be the case ?

[ December 07, 2008: Message edited by: sid wagle ]
[ December 08, 2008: Message edited by: sid wagle ]
 
Vijitha Kumara
Bartender
Posts: 3973
24
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources. And Welcome to JavaRanch
[ December 07, 2008: Message edited by: Vijitha Kumara ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sid:
Declaring the method doThings() as static would make the class thread safe.
Could any one shed some light on why this would be the case ?


Currently, doThings() is an instance level method, so the lock taken (as it is synchronized) before executing the method will be for the instance on which this method is executed.
If two threads have different instances of the class TestThread then they can concurrently execute the method doThings() and hence modify the static variable x.
On the other hand, if doThings() is static then no two threads can execute the method concurrently.

BTW, we have a separate forum for SCJP questions
 
chinmay mohanta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Answer is very correct. Adding some more detail to the above explanation.
Explanation:
Consider the Scenario.
Thread th1=new TestThread();
Thread th2=new TestThread();
Th1.start();
Th2.start();
Since the method doThings() is an instance method, making the method only synchronized would not stop this method to be called simultaneously. This scenario will most likely corrupt the static variable x.
On the other hand if doThings(), will be declared as static the synchronization will be very much take into effect since both the Threads will be executing the same method.
[ December 11, 2008: Message edited by: chinmay mohanta ]
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two threads accessing a static synchronized method will block each other.


[ December 11, 2008: Message edited by: Sudipto Shekhar ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!

Similar Threads