aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 2 more thread questions 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 "2 more thread questions" Watch "2 more thread questions" New topic
Author

2 more thread questions

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
Just when I thought I understood threads, I came across these practice questions, and did not understand the given answer. What would YOUR answer be and why?

QUESTION #1
----------------
5. public class Lockdown implements Runnable {
6. public static void main(String[] args) {
7. new Thread(new Lockdown()).start();
8. new Thread(new Lockdown()).start();
9. }
10. public void run() { locked(Thread.currentThread().getId()); }
11. synchronized void locked(long id) {
12. System.out.print(id + "a ");
13. System.out.print(id + "b ");
14. }
15. }

What is true about possible sets of output from this code?
A. Set 6a 7a 7b 8a and set 7a 7b 8a 8b are both possible.
B. Set 7a 7b 8a 8b and set 6a 7a 6b 7b are both possible.
C. It could be set 7a 7b 8a 8b but set 6a 7a 6b 7b is NOT possible.
D. It could be set 7a 8a 7b 8b but set 6a 6b 7a 7b is NOT possible.


QUESTION #2
----------------
5. class NoGo implements Runnable {
6. private static int i;
7. public synchronized void run() {
8. if (i%10 != 0) { i++; }
9. for(int x=0; x<10; x++, i++)
10. { if (x == 4) Thread.yield(); }
11. }
12. public static void main(String [] args) {
13. NoGo n = new NoGo();
14. for(int x=0; x<101; x++) {
15. new Thread(n).start();
16. System.out.print(i + " ");
17. } } }

Which is true?
A. The output can never contain the value 10.
B. The output can never contain the value 30.
C. The output can never contain the value 297.
D. The output can never contain the value 820.
E. Making the run method un-synchronized will NOT change the possible output.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
What were the given answers?

Use codetags.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 797
    
    1
For question 2,
The scenairo is 100 threads increasing i.
Thread.yield() means a thread pasues for a while to let another thread to run. So, synchronized or non-synchronized run method will not change the possible output.

Think of a nice possible scenairo: one thread executes one at a time and this thread yields and let the main thread to execute. It is less likely, but still possible.
thread 0 prints 10,
thread 1 prints 20,
thread 3 prints 30 and so on...

So, the possible ouptuts contains 10,20,30 .... but never 297.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 797
    
    1
For question 1, the main concept bebind is :

a) is not possible because there are actually two threads running, as well as the main thread. The main thread won't execute the run method. The two threads created in line 7 and 8 are the only threads executing the run method synchronously.
In a) , it prints the thread ID's 6, 7, 8. It is possible that the two threads created in line 7 and 8 have ID 7 and 8. It is possible the main thread has an ID 6. But the main thread won't execute the run method.

b) 7a 7b 8a 8b is possible : Thread ID 7 executes first and then thread ID 8 executes next.
6a 7a 6b 7b is possible. Suppose The threads created in line 7 and 8 are assigned to ID 6 and 7. In line 7 and 8, the two threads do not share the same Lockdown instance. They lock their own Lockdown instances respectively. Thread 6 acquires the lock of its own Lockdown object. Thread 7 does not need to wait for thread 6 to release the lock because Thread 7 locks its own Lockdown object. It is just like a real life example: I have my own folk to eat my lunch and you have your own folk to eat your lunch. We can eat our own lunch at the same time. I don't need to wait for your folk or vice versa.


Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Rachel,

In first question, the trick is on the lines 7,8 and 10. In line number 10, we are passing the id of thread calling the run method. Thread id is a unique number, assigned to a thread and does not change during the lifetime. Considering this, option a is incorrect because it is displaying three ids while only two threads are accessing the locked method.
As both the treads are taking lock on different objects, they might run in parallel so 7a 7b 8a 8b and 6a 7a 6b 7b both are possible, which is answer b.

In second question, the trick is in line number 7,9,10,15. In this case, synchronized run method, makes sure that only one thread is executing at a time in it. If we make it unsynchronized race condition would occur in i++ operation and result would become unpredictable. so the answer E is incorrect.
In line number 10, on the increment of 5 thread yields (move to runnable state and might get to running state again before any other thread starts running) and on the increment of next 5, thread completes the execution. So numbers starting from 10 in the multiple of 5, can appear in the result. Considering this only option C violates this. That is how the correct answer turns to be C.

Regards
Salil Verma
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
The ANSWER to question #1 is B. What I do not understand is how 6a 7a 6b 7b IS POSSIBLE ???

The ANSWER to question #2 is:" Option E is correct. To make sure that line 16 is never executed when an invocation of the run method is partially complete, it (line 16) should be placed inside the run method." I just don't seem to understand the logic of this one... (maybe it is too late and I need to go to bed!!)
Sresh Rangi
Ranch Hand

Joined: Nov 28, 2012
Posts: 47
    
    2
In question #1, one thread can output "6a", then "6b". Another thread can output "7a", then "7b". Because each thread is synchronized on a different object, the outputs from each thread can be interleaved freely. So any output with 6b after 6a, and 7b after 7a is possible.

In question #2, each thread except the main thread is synchronized on the same object. Only one thread can update i at one time, but the main thread can read the value of i while it's doing so. With or without the synchronization, any of the values of i can be outputted.
 
jQuery in Action, 2nd edition
 
subject: 2 more thread questions