This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switching of threads !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switching of threads !!!" Watch "Switching of threads !!!" New topic
Author

Switching of threads !!!

tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
Hi all !
The following is a very simple prog of a thread:


Ouptut :It shows randomly the main and child thread contents. (In windows)

Problem : Here ,my prob is that :
"Is the switching between threads is done by Operating System
by giving time slice ?? If yes,then the thread is in which state in "Thread Transition."while another thread is running.

Please help me out.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Yes, the determination of which thread will execute at any given time is up to the underlying OS. As a programmer, you have little to no control over which thread is executing at any given time.

When a thread is created, it goes into the "Ready to Run" state. The OS then chooses a thread from that state and starts it running. At that point, the thread is in the "Running" state. Eventually, the running thread may use up its time-slice. At that point, the OS will grab the thread in the "Running" state and put it into the "Ready to Run" state. Then, another thread from the "Ready to Run" state is put into the "Running" state.

Threads can also halt for other reasons rather than using up a time-slice. Perhaps the thread completed its task. At that point, the thread would go to the "Terminated" state. Or perhaps the thread is waiting for some I/O to occur - the thread may go to a "Blocked" state.

Many people use slight variations of these state names, but the concepts are usually identical. When you have multiple threads executing, only one can be in the "Running" state at a time. This thread is exchanged with other threads in the "Ready to Run" state as the underlying OS sees fit.


SCJP Tipline, etc.
tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
Thanx Sir !!

But i still have some doubts , plz clear that also

1) Here,in the example the priority of both threads are 5 ,if any of the therads' priority is changed then the result will randomly show the main and child contents or not ??

2) Can we change the priority of main thread bcoz in main we cant call the instance method setPriority().

3) If in SCJP this type of question is asked vat should i reply bcoz i think in linux there is no time slice algorithm is implemented for multithreading.

Thanx
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

1) Here,in the example the priority of both threads are 5 ,if any of the therads' priority is changed then the result will randomly show the main and child contents or not ??


As far as I know, setting thread priorities is a "suggestion" for the underlying OS. You don't know if the OS will truly follow your priorities or not. One would hope it would, but I don't know that we're guaranteed that behavior. I don't believe there are any thread priority questions on the exam.


2) Can we change the priority of main thread bcoz in main we cant call the instance method setPriority().


In the example you've given, I don't believe you can. The method setPriority is defined in class Thread. As your class doesn't extends Thread, you can't invoke that method.


3) If in SCJP this type of question is asked vat should i reply bcoz i think in linux there is no time slice algorithm is implemented for multithreading.


As a side note, please don't type like this: "vat should i reply bcoz". I'll seldom answer such questions because I don't want to expend the effort of trying to read what you wrote.

However, if this question were asked on the SCJP exam, you would have to say that the output is non-determinable. There will be no questions that detail what operating system the application is running on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switching of threads !!!
 
Similar Threads
Threads
Threads
Main Thread is Alive ?
What are those two threads???
Threads problem !!!!