choose correct answer 1) The value of j could never be 8 2) an error at line 3 cause come to fail 3) t1.run() and t2.run() may execute concurrently 4) t1.run() will definitely return before t2.start() is called in my thought 1) and 4) are correct.. but i'm not sure those two threads are not sharing private member variable.. plz correct me~~~ [ Ajith added code tags ] [This message has been edited by Ajith Kallambella (edited October 18, 2000).]
Bin Zhao
Ranch Hand
Joined: Oct 04, 2000
Posts: 73
posted
0
I agree with you. 1) and 4) are correct.
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
I agree that 1 is correct, but I am not sure about 4. The thread will start, but it won't execute the run until the lock is released by Thread 1. So when it says Thread 1 will return before the start method is called I think is incorrect. The method run won't get executed until Thread 1 is finised is correct, but the start method will still be called. However, I am VERY new with threads, so I could easily be wrong. I would say the only answer is A. 3 is wrong since run is synchronized. Line 3 should compile fine since I believe you can make any methods synchronized, even the run method (which seems kind of silly since doesn't that defeat the purpose of creating threads). Any thoughts from anyone else to help clarify this. As I said, I just started studing threads, so this is pretty new to me.
deekasha gunwant
Ranch Hand
Joined: May 06, 2000
Posts: 396
posted
0
Hi all, I feel only 3 is correct.
Reason being - There are two objects of class A .namely t1 and t2. when t1 enters its run method then it acqires the object lock i.e. a lock on object pointed by t1. now t2 can always enter in to the run method of another object. and since both the threads can run concurrently there may be a chance that the var j may become 8.
so my views are completely contradictory with others . can gurus give us the last word. regards deekasha
satishind Reddy
Ranch Hand
Joined: Oct 17, 2000
Posts: 33
posted
0
Hi Deekasha, I am also suprting your reasoning... Here i modified code a little. it is giving out Different out put Each time i run the program.
Result is some times i am getting below mentioned result Thread[Thread-0,5,main] Thread[Thread-1,5,main] 8 8 some times i am getting below mentioned result Thread[Thread-0,5,main] 4 Thread[Thread-1,5,main] 4 from this result what i understand is threads are running concurently.... if i am wrong please correct me.... Regards Satish
[This message has been edited by Ajith Kallambella (edited October 18, 2000).]
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5781
posted
0
I think the correct answer is just 3. Note that since the variable 'j' is declared <code>static</code>, all instances of this object will share the same variable. This means no matter how many threads you create, when they are run, they are all accessing the same variable 'j'. This is where deekasha, your reasoning about different objects loses ground Eventhough there are two different objects with their own run method, they are accessing the same varible!! One of the very important things you should remember about threads is that the order in which threads are run is vendor dependant. When you call the <code>start></code> method on a thread, the thread gets added to a pool of "ready to run" threads. It is upto the thread scheduler( vendor specific ) to decide which one gets picked first. Various implementations of schedulers use various criterion to decide who gets to run first - thread priority being one such factor. So, if you start multiple threads, there is no way to tell which one runs first. They can run concurrently ( multiprocessor machine ), or one after the other. That's what this question is all about. If you compiled this program and run on various different platforms, you will get different results!! because each of them have their own thread scheduling policy. Infact, even on the same machine, as Satish has noted, it can produce different results. Now looking at the answers, though (1) looks like a possibility, note that each thread has its own run method!!. So the synchronized word is redundant here and has no effect on the behaviour. Answer (2) is wrong because the code compiles fine. Answer (4) uses conclusive tone and hence it is wrong. Hope that helps, Ajith [This message has been edited by Ajith Kallambella (edited October 18, 2000).]
Open Group Certified Master IT Architect.
Sun Certified Architect(SCEA).
Udayan Naik
Ranch Hand
Joined: Oct 18, 2000
Posts: 135
posted
0
I'm of the opinion that only 3) is right.This is because 1)The program is creating 2 DIFFERENT OBJECTS of the class A.So each thread has to obtain the lock on a different object.This is very much possible since there is only 1 thread obtaining the lock. 2)Since j is a static variable,there is only 1 j which is common to both objects. Let's consider the scenario where thread t1 obtains lock for an object of class A , enters run() and executes j*=2. So now j=4. Now at this moment thread t2 obtains lock for another object of class A , enters run() and executes j*=2. Since j is static , now j=4*2=8. So applying this logic , let's dwell on the options 1)The value of j could never be 8 - FALSE.Not always,as explained above. 2)an error at line 3 cause code to fail - FALSE. Modifiers(barring Access modifiers) are NOT part of method signature.Thus run() can be synchronized. 3)t1.run() and t2.run() may execute concurrently - TRUE as explained above. 4)t1.run() will definitely return before t2.start() is called - FALSE.Since there are 2 objects in this case ,t1.run() and t2.start() are not concerned with each other.In fact,even if there had been only 1 object this would NOT have been true , since t2.start() will be called and will put the thread in READY state , but t2.run() WILL NOT be called until t1.run() returns , since run() is synchronized. I hope i have been clear enough.Feel free to point out any mistakes. ------------------ Come on in !! Drinks are on the house in the Big Moose Saloon !! [This message has been edited by Udayan Naik (edited October 18, 2000).]
Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform