This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line! See this thread for details.
Hi all, I don't understand the possible reuslts of the following question of Dan's study guide.
What are the possible results of attempting to compile and run the program? a. Prints nothing. b. Prints: [T1,A][T2,B]. c. Prints: [T1,B][T2,B]. d. Prints: [T2,B][T1,A]. e. Prints: [T2,A][T1,A]. f. Compiler error. g. Run time error. h. None of the above. The right answers are b and d. I tried it out and this is right. But I don't understand it. There is only one instance of class B. Class B has a member variable called s1, which is set in the synchronized method m1. If this variable is set to "A" or to "B" and the yield method is called, I think it must be overwritten in the next call of m1 of the second thread. So I expected, that the possbile results are c and e. But this does not happen. Can anybody explain this to me ? Thanks in advance. [ Jess added UBB [code] tags to preserve whitespace, check 'em out! ] [ September 08, 2003: Message edited by: Jessica Sant ]
Hi Michael It's so because in here the method is synchronized. This would ensure that the second method invocation is done only after the first has completed. The yield() method according to me won't affect the execution I guess.
In my opinion, it's because the run() method in A class is not synchronized, so there's no guarantee which thread runs first. Secondly, when a Thread enters m1() method of B, an attempt to yield() is made to let a thread of the same or higher priority run. I don't believe this attempt is successful because we're inside the synchronized method of the only object of B, and the lock should be relinquished. So to me, the output varies from b. to d. due to run() not being synchronized and just one instance of B. Please correct me, this question looks interesting.