• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 more thread questions

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What were the given answers?

Use codetags.
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!)
 
Ranch Hand
Posts: 59
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic