• 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

moka question about thread

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I've a dubt about a moka

Considering the following program and choose the correct option describing its behaviour:

class ThreadTest{

public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(){
public void run(){
System.out.print("t1 ");
}
};
Thread t2=new Thread(){
public void run(){
System.out.print("t2 ");
}
};
t1.start();
t1.sleep(1);
t2.start();
t2.sleep(1);
System.out.println("main ");
}

}

A) it prints t1 t2 main
B) it prints t1 main t2
C) it prints main t2 t1
D) this program results in runtime error
E) this program throws an exception

For what I got about concurrency answers A, B, C should be all possible, anyway the book explaination states as follow:

"Answer A t1, t2, main
When a new thread is created, it is in new state. Then it moves in runnable state. Only from the runnable state can the thread go to the timed waiting_state after calling sleep(). Hence, before executing sleep() the run method from that thread is called. So the program print t1, t2, main"

Is the explaination correct?
Isn't possible the timeout expires before the scheduler chose which thread runs? (so that it could select the main thread before the two spawned threads)


Best regards



 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is about Thread methods. The sleep method is a class method and it causes the currently executing thread to sleep for the specified number of milliseconds.
As current thread is the main thread, it fits the book explanation. The fact that we can call a method class using an instance variable is just used to confuse.

Best regards

 
Luca Cazzaniga
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your reply, but I can ask you to enlight me if a specific case is possible.
The sleep method involves the transit of the current (main) thread in wait_timeout for one millisecond (that's ok)
but can't the timeout expire before the scheduler chose the next thread to process?
In that case the scheduler could choose to terminate the main thread before to process the spawned threads being the execusion order not deterministic.
Did the sleep method force an execution order?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luca Cazzaniga wrote:Thanks a lot for your reply, but I can ask you to enlight me if a specific case is possible.
The sleep method involves the transit of the current (main) thread in wait_timeout for one millisecond (that's ok)
but can't the timeout expire before the scheduler chose the next thread to process?
In that case the scheduler could choose to terminate the main thread before to process the spawned threads being the execusion order not deterministic.
Did the sleep method force an execution order?




You are correct. The sleep() method is a static method and affects the calling thread only -- it doesn't affect the thread of the thread object which is used to call that static method. And I read that quote three times, and I am still not sure if I understood it....

Henry
 
Luca Cazzaniga
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harry, sorry for my English..

There are three thread involved in the execution:
the main thread and the two spawned thread (printing a and b)

Is the following sequence of events possible?

main spawns thread t1

thread t1 pass in runnable state (ready)

main goes to sleep for 1 millisecond

the thead main awakes and becomes runnable

the scheduler choose main as running thread

main spawns thread t2

thread t2 pass in runnable state (ready)

main goes to sleep for 1 millisecond

the timeout expires and the thead main awakes (now it's runnable)

the scheduler choose main as running thread

the main terminates

the scheduler chooses t2 as running thread

the scheduler chooses t1 as running thread


I'm not sure about what sleep() is supposed to do, shouldn't it send to sleep only the current thread?

In the explaination it seems to force the scheduler to choose the next ready thread to run at the state translation moment, but isn't it matter of vm scheduler implementation/policies?

Thanks a lot for your time.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luca Cazzaniga wrote:Hi Harry, sorry for my English..

There are three thread involved in the execution:
the main thread and the two spawned thread (printing a and b)

Is the following sequence of events possible?

main spawns thread t1

thread t1 pass in runnable state (ready)

main goes to sleep for 1 millisecond

the thead main awakes and becomes runnable

the scheduler choose main as running thread

main spawns thread t2

thread t2 pass in runnable state (ready)

main goes to sleep for 1 millisecond

the timeout expires and the thead main awakes (now it's runnable)

the scheduler choose main as running thread

the main terminates

the scheduler chooses t2 as running thread

the scheduler chooses t1 as running thread


I'm not sure about what sleep() is supposed to do, shouldn't it send to sleep only the current thread?

In the explaination it seems to force the scheduler to choose the next ready thread to run at the state translation moment, but isn't it matter of vm scheduler implementation/policies?

Thanks a lot for your time.




I am not sure what extra clarification you are looking for. I already agreed with you. I already stated that you are correct. And the quote from the book didn't really make much sense.


But.... okay.... Yes, that sequence is possible. Yes, sleep() only affects the current thread. And yes, the explanation seems to draw a conclusion that isn't true...

Henry
 
Luca Cazzaniga
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok

Thanks again

Luca
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic