File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Behavour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Behavour" Watch "Thread Behavour" New topic
Author

Thread Behavour

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
please consider this thread:

C:\Java\EigeneJavaProgramme java Test34
MAIN-METHOD 0
MAIN-METHOD 1
.............
MAIN-METHOD 227
THREAD 0
THREAD 1
........
THREAD 1999
MAIN-METHOD 229
.........
MAIN-METHOD 2999
When I start the programm all over again it prints:
C:\Java\EigeneJavaProgramme java Test34
MAIN-METHOD 0
MAIN-METHOD 1
.........
MAIN-METHOD 348
THREAD 0
THREAD 1
.........
THREAD 1999
MAIN-METHOD 349
.........
MAIN-METHOD 2999

Therefore two questions about Thread behavour:
1. Why this difference in the execution time?
In the second run of Test24 it seems the computer gives the main method
more execution time than before.
2. What about the interaction of Thread and main. It seems that the start of
A thread interrupts main() for the time a thread is fully executed. Why
isn’t there a switchback from main to Thread from Thread to main etc.
it is only main() – Thread interrupts main until finished – programm main
continues.
Thanks for your answers.
Thomas
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
There is no reason to expect any kind of behaviour whatsoever. The two threads are of the same priority and the processor can allocate time between the threads any way it wants. If you want the main thread to get a bit of time once the second thread is running you can perform a Thread.yield() or Thread.sleep() or some kind of asynchronous I/O operation.
Some implementations may schedule threads differently such as giving a few milliseconds cpu to a thread before giving another thread a few milliseconds, and so on. Also in a few milliseconds you can do a lot of operations on modern cpus, and so a thread may complete in its time slice.
Running on a two, or more, processor system may give rather different results.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But also be aware that Thread.yield() can end up being a no-op; the correctness of your program shouldn't depend on a guarantee that this causes some other thread to get CPU time.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Barry:
From Barry's post:
There is no reason to expect any kind of behaviour whatsoever. The two threads are of the same priority and the processor can allocate time between the threads any way it wants.

Which two threads you are pointing to? There is only one thread that is t. My understanding was that when run method will come back with a return, the next statement following start() statement will be executed. Why is it behaving like a separate thread?
Another question: assume that there are two explicit threads: t1, t2. If t1 has more priority over t2, is that an assurrance that t1 will completely finish before t2 will start executing...or we need a synchronized method to achieve that. By the way, can we designate the run method itself as synchronized so that one thread is finished before second starts. Or we need a deeper level synchronized method called from run() to accomplish such behaviour.
Thanks
Barkat
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
t.start() starts a separate thread. The original program is its own thread so there are two threads running.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Barkat, Thomas said it, thanks Thomas.
Ron, thanks too. Thead.yield() is just a hint to the JVM to schedule other threads, if it wants to.
Barkat, regarding your second question on thread priority scheduling, I suggest that you raise it in another thread.
-Barry
[ September 01, 2002: Message edited by: Barry Gaunt ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Barkat Mardhani:

Another question: assume that there are two explicit threads: t1, t2. If t1 has more priority over t2, is that an assurrance that t1 will completely finish before t2 will start executing...or we need a synchronized method to achieve that. By the way, can we designate the run method itself as synchronized so that one thread is finished before second starts. Or we need a deeper level synchronized method called from run() to accomplish such behaviour.

Setting a thread at a high priority will not guarantee that it finishes first. Try it by editing out the line after #1. To enable threads to accomplish tasks in a certain order, you can use join(). The thread that invokes join() will be allowed to finish first, as Jim mentioned in another, ah, thread...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Behavour
 
Similar Threads
Thread with synchronized run() method and sleep()
Strange behavour in loop
finally { } clause in Exceptions
Thread with synchronized run() method and sleep()
When to synchronize a method?