• 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

Thread Behavour

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
t.start() starts a separate thread. The original program is its own thread so there are two threads running.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic