• 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

join()

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Output of this program:

0 Fiji
0 Jamaica
1 Fiji
1 Jamaica
2 Jamaica
2 Fiji
3 Fiji
4 Fiji
3 Jamaica
5 Fiji
4 Jamaica
6 Fiji
5 Jamaica
DONE! Fiji
6 Jamaica
DONE! Jamaica

http://www.edumax.com/java-basics-thread-join.html

Using A.join() within a thread is tantamount to making the current thread sleep until thread 'A' completes execution.



In this program , a.join must cause another thread stops , until a dies.

Then the output must be this :


0 Jamaica
1 Jamaica
2 Jamaica
3 Jamaica
4 Jamaica
......

Why isn't that?


 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:
In this program , a.join must cause another thread stops , until a dies.



a.join() causes stop of the CURRENT thread (this thread that invokes a.join() )
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only the main thread will wait for thread 'a' to complete its execution. and that is because you have invoked join from the main thread. Try having a print statement in your main method after you invoke join, it would execute after thread 'a' completes.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Another question:

However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.



http://java.sun.com/docs/books/tutorial/essential/concurrency/join.html

1-Is it only for times that we specify a time in join() method?

2-Is it right for wait() method?

3-Is it the Os or is it the JVM choose which thread to run?

4-Who is "thread scheduler" here? is it part of OS or JVM?


 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:

1-Is it only for times that we specify a time in join() method?

2-Is it right for wait() method?

3-Is it the Os or is it the JVM choose which thread to run?

4-Who is "thread scheduler" here? is it part of OS or JVM?



Sorry but I could not get your first and second question but for the last two, see this and this thread. Hope this help.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we use a.join(), the current thread will stop until thread a be finished
Now the question is: Is it possible the current thread doesn't stop?


Can the similar situation happened for wait() method?

-From what I understand:

We have a Java program , Thread are created by JVM, But this is "thread scheduler" that decides which thread must run. regardless of the methods that we use or priority .Right?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.cs.umd.edu/class/fall2007/cmsc132/lectures/26-Threads-6p.pdf

Page 4

Scheduler is part of OS or Java Virtual Machine (JVM)

May you explain more about mechanism of Scheduler?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
V

M to OS Thread Mapping

The way Java threads are mapped to OS threads is up to the JVM and its use of multiple processors on a machine. Different JVMs use different strategies:

Some JVMs use green threads, running all Java threads in one native OS thread (called n-to-one mapping). Sun's HotSpot JVMA website external to this site uses native threads, which may execute in parallel on a multi-CPU machine. On Solaris, Java threads are not bound permanently to the same native threads but are remapped by the scheduler (in an n-to-m mapping).



1-What is thread mapping?

2-What is native thread?What its difference with green thread?
 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, it depends on the implementation of the JVM. Modern JVM’s do not use their own but instead rely on the underlying operating system. This provides better performance as well. How the JVM maps Java threads to OS threads varies a bit, but in general each java thread is mapped on 1 native thread in the OS.
Regarding green threads, early implementation of JVM were used them as the only threading model. But now green threads have been removed completely from java code and are no longer supported. Modern JVM’s are now use native threads instead of them.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a discussion here about thread scheduler :

http://forums.sun.com/thread.jspa?threadID=5312040



1. The java threads are scheduled by the jvm

No they are not. Certainly not in the Sun VM. And I would doubt that for any recent VM on a desktop OS.

The OS does it.



Sure? Until I know (I'm not an expert at all) the jvm is scheduled by the OS, but the java threads are scheduled by the jvm. This way if you are running on a machine a jvm with 5 java threads and the mysql daemon, for example, OS will schedule the jvm as one process and the mysql daemon as another process. In the cpu time assigned by the OS to the jvm, it will schedule between the 5 java threads.

At best the VM will set the priority. Scheduling is by the OS.






Now,Who schedule threads? JVM or OS? what is the relationship between these of Thread scheduling?

Is it true that:

if one OS thread is mapped to two or more java thread then obviously it is the responsibility of JVM to decide which thread to run when the OS thread is running

if there is direct mapping like one OS thread == one Java thread, then ofcourse OS scheduler decides which thread to run
 
reply
    Bookmark Topic Watch Topic
  • New Topic