aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Threads" Watch "Question on Threads" New topic
Author

Question on Threads

Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
I understand that each thread will have its own copy of instance variables but is this true for Static variables as well.

I have a following question. Its correct answer is a) but I feel it should be b). Please clarify.



Answers:

a) Each thread will get a unique number.
b) The uniqueness of the number among different Threads can't be guaranteed.


SCJP 1.4 - 95%
Manoj Kumar Sikhakolli
Greenhorn

Joined: Sep 22, 2005
Posts: 15
Wher is the thread here?
Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
Sorry, I missed a part of the question, here it goes :

Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code?

You can also view the complete question here
Manoj Kumar Sikhakolli
Greenhorn

Joined: Sep 22, 2005
Posts: 15
HI sharn..

First of all threads does not own a separate copy of static variables as by the name static we mean that it is only one for the Class n not for objects.. so it is clear that only one copy of the static variable will be available.

and the way to keep it(static) in control when accessed by multi threads is Synchronisation .. here also the static varibale is modified only inside a synchronized method n hence there is no way a thread can change the value when sum other thread is changing.. so the values we get would be unique for each thread.

Hope this clears your doubt
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023


The synchronized keyword can be used to mark a statement or block of code so that only one thread may execute an instance of the code at a time.

Also check this thread.
[ June 08, 2006: Message edited by: wise owen ]
 
 
subject: Question on Threads
 
Similar Threads
Word Counter
Different ways of implementing Thread , whats the difference based on output
Thread
question about thread at J@Whiz1.4
threads