• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam LAb Question on THreads

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is it possible that main thread runs the line 1 and line2 and line3 first and then line 4.

b thread may start after the main thread
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is incomplete. As for your question, its possible that main thread executes before any other thread that you start.

[Edit] Now that you've posted the complete code, yes its possible that main thread executes before b thread (even though the possibility is very less but its possible). If b thread starts before the main thread, there will be a deadlock..
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

UPDATED the code

As for your question, its possible that main thread executes before any other thread that you start...


THe answer is exam lab is that
there would be no output .
As when thread b starts ,it acquires lock on a.then,when it goes to it's doTask2 method,it joins and waits till thread main completes.
Thread main too needs lock on a to call line2.

The case when main thread runs before b thread is not taken into account
So, answer in Exam Lab is wrong
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you execute the code? There is a sleep() method!
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know that Thread.sleep(1000)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.

i ran the program and no output appears

but ,i still have one confusion that
what if


are executed first than any other code including Thread.sleep(1000) in main thread
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:i know that Thread.sleep(1000)

but ,i still have one confusion that
what if


are executed first than any other code including Thread.sleep(1000) in main thread

How can the main thread cross without executing the sleep() method?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,i got it
thanks Abimaran
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can be output from the program. Even if main method sleeps for 1 second, the other thread might not get an opportunity to execute. The chances are very very low but its possible...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:There can be output from the program. Even if main method sleeps for 1 second, the other thread might not get an opportunity to execute. The chances are very very low but its possible...

Agreed! But, these kind of question won't be in the exam, for sure, you can predict the output without this kind little possibility!
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say you CAN face these kind of questions on the exam. For the exam the answer would be that there would be either no output or "12" might be the output...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic