File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join() in Threads

 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code -:



I ran it several times to check out what kind of output I would get.

Whenever T1 finished execution, the main thread would first commence its run(), even for very few iterations, and then only T2 would produce output.

For example,

T1 : 1
T1 : 2
T1 : 3
T1 : 4
T1 : 5
T1 : 6
T1 : 7
T1 : 8
T1 : 9
T1 : 10
T1 : 11
T1 : 12
T1 : 13
T1 : 14
T1 : 15
T1 : 16
T1 : 17
T1 : 18
T1 : 19
T1 : 20
T1 : 21
T1 : 22
T1 : 23
T1 : 24
T1 : 25
T1 : 26
T1 : 27
T1 : 28
T1 : 29
T1 : 30
T1 : 31
T1 : 32
T1 : 33
T1 : 34
T1 : 35
T1 : 36
T1 : 37
T1 : 38
T1 : 39
T1 : 40
T1 : 41
T1 : 42
T1 : 43
T1 : 44
T1 : 45
T1 : 46
T1 : 47
T1 : 48
T1 : 49
T1 : 50
T1 : 51
T1 : 52
T1 : 53
T1 : 54
T1 : 55
T1 : 56
T1 : 57
T1 : 58
T1 : 59
T1 : 60
T1 : 61
T1 : 62
T1 : 63
T1 : 64
T1 : 65
T1 : 66
T1 : 67
T1 : 68
T1 : 69
T1 : 70
T1 : 71
T1 : 72
T1 : 73
T1 : 74
T1 : 75
T1 : 76
T1 : 77
T1 : 78
T1 : 79
T1 : 80
T1 : 81
T1 : 82
T1 : 83
T1 : 84
T1 : 85
T1 : 86
T1 : 87
T1 : 88
T1 : 89
T1 : 90
T1 : 91
T1 : 92
T1 : 93
T1 : 94
T1 : 95
T1 : 96
T1 : 97
T1 : 98
T1 : 99
T1 : 100
main : 1
main : 2
main : 3
main : 4
main : 5
T2 : 1
T2 : 2
T2 : 3
T2 : 4
T2 : 5
T2 : 6
T2 : 7
T2 : 8
T2 : 9
T2 : 10
T2 : 11
T2 : 12
T2 : 13
T2 : 14
T2 : 15
T2 : 16
T2 : 17
T2 : 18
T2 : 19
T2 : 20
T2 : 21
T2 : 22
T2 : 23
T2 : 24
T2 : 25
T2 : 26
main : 6
main : 7
main : 8
main : 9
main : 10
main : 11
main : 12
main : 13
main : 14
main : 15
main : 16
main : 17
main : 18
main : 19
main : 20
main : 21
main : 22
main : 23
main : 24
main : 25
main : 26
main : 27
main : 28
main : 29
main : 30
main : 31
main : 32
main : 33
main : 34
main : 35
main : 36
main : 37
main : 38
main : 39
main : 40
main : 41
main : 42
main : 43
main : 44
main : 45
main : 46
main : 47
main : 48
main : 49
main : 50
main : 51
main : 52
main : 53
main : 54
main : 55
main : 56
main : 57
main : 58
main : 59
main : 60
main : 61
main : 62
main : 63
main : 64
main : 65
main : 66
main : 67
main : 68
main : 69
main : 70
main : 71
main : 72
main : 73
main : 74
main : 75
main : 76
main : 77
main : 78
main : 79
main : 80
main : 81
main : 82
main : 83
main : 84
main : 85
main : 86
main : 87
main : 88
main : 89
main : 90
main : 91
main : 92
main : 93
main : 94
main : 95
main : 96
main : 97
main : 98
main : 99
main : 100
T2 : 27
T2 : 28
T2 : 29
T2 : 30
T2 : 31
T2 : 32
T2 : 33
T2 : 34
T2 : 35
T2 : 36
T2 : 37
T2 : 38
T2 : 39
T2 : 40
T2 : 41
T2 : 42
T2 : 43
T2 : 44
T2 : 45
T2 : 46
T2 : 47
T2 : 48
T2 : 49
T2 : 50
T2 : 51
T2 : 52
T2 : 53
T2 : 54
T2 : 55
T2 : 56
T2 : 57
T2 : 58
T2 : 59
T2 : 60
T2 : 61
T2 : 62
T2 : 63
T2 : 64
T2 : 65
T2 : 66
T2 : 67
T2 : 68
T2 : 69
T2 : 70
T2 : 71
T2 : 72
T2 : 73
T2 : 74
T2 : 75
T2 : 76
T2 : 77
T2 : 78
T2 : 79
T2 : 80
T2 : 81
T2 : 82
T2 : 83
T2 : 84
T2 : 85
T2 : 86
T2 : 87
T2 : 88
T2 : 89
T2 : 90
T2 : 91
T2 : 92
T2 : 93
T2 : 94
T2 : 95
T2 : 96
T2 : 97
T2 : 98
T2 : 99
T2 : 100


Here's my question. Is there any guarantee that main (the thread being joined) will execute *before* any other thread displays any kind of output/performs any operation/executes its run()?
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pritish, the program is running perfectly as expected.

As per my understanding, you want main thread to run independently and before T2, if that is the case you can just start T2 after main.run()

Regards,
Don,Redd..
 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already tried what you said, Don. I can place the call to t2.start(); before the t1.join(); call.

That would make T1 and T2 execute in tandem first.

My question was more about what guarantees does the JVM give to us here regarding the execution of main's run() before or after T2's run()
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pritish Chakraborty wrote:My question was more about what guarantees does the JVM give to us here regarding the execution of main's run() before or after T2's run()

Case 1:

t1 executes before t2 and main.

Case 2:

t1 executes before t2 and t2 executes before main.

Case 3:

Can not get exact execution order. It will be t1,t2,main or t2,t1,main or main,t2,t1 ...

Same for the following
Case 4:


Note: main.run() is executed by main thread(creates by JVM when it starts executing your standalone java program) and not the thread you have created at line no. 04.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Gaurang kumar explained above, THERE IS NO GUARANTEE by the jvm. it can be possible that T2 thread runs its run() method first before the main thread executes call to main.run().
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you can take a look at Thread priorities.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic