Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization problem using Priority  RSS feed

 
Zulfi Khan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a java program which creates 3 threads. I am trying to print thread names from 1-100 by assigning priorities but i am getting jumbled output. I want the thread1 to run first to completion , then thread thread2 and so on. My code is:



I am getting following output:


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



Some body please guide me.

Zulfi.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zulfi Khan wrote:Hi,
I have a java program which creates 3 threads. I am trying to print thread names from 1-100 by assigning priorities but i am getting jumbled output. I want the thread1 to run first to completion , then thread thread2 and so on.

By its very definition, threads are asynchronous, and you only have limited ability to give "hints" to the JVM as to the precedence of each thread. Why are you trying to use threads at all?

 
Zulfi Khan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to give an example of thread priority and how to make sure that threads can keep processor control. A thread with MAX_PRIORITY should not let other threads to run but its not working. Kindly guide me.

Zulfi.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zulfi Khan wrote:A thread with MAX_PRIORITY should not let other threads to run


Can you point to where in the documentation it says that?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are so many variables that will affect the running of threads. For instance, my machine has 4 cores and 8 hyperthreads and given that the JVM attempts to split threads up among the cores, you'd need more than 8 threads running in order to force some amount of task swapping. Additionally you'd need each thread to be running a very CPU intensive set of code in order to consume 100% of the thread's allotted cycles. Even then, as I said, a priority is a suggestion to the JVM.

Priorities range from 1 to 10 inclusive (low to high). I wrote a program that allocates 10 threads, each with a different priority. After running for 10 minutes these were the counts I got for each priority. Of note  is priority '2' which is the next to lowest almost doesn't run at all, which I find very strange. There is almost no difference for threads 7, 8, 9, and 10.



Here's my test code:
Note, if you run this on your own machine you will probably have to reduce "upper" on line 84. My machine is over-clocked so I had to have a fairly large number.

 
Zulfi Khan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi my friend,

Thanks for your work. But sorry, I cant understand how its related to my work. You are just telling me about priorities. Priority is one way to solve this problem. Its a mutual exclusion prob. I have a 'for' loop which contains critical section and i want it to be accessed one by one. Java has synchronized block and normally it is used to solve this sort of prob. But it should be solved through priority also. Theoretically its true. Higher priority jobs have privilege over lower priority jobs and that's why we use priority concepts. Otherwise there is no need for it. I would now try synchronized block to solve this problem.

Zulfi.

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zulfi Khan wrote:Priority is one way to solve this problem.

There's nothing about priority that says a higher priority thread will finish before the next lower thread starts.
 
Zulfi Khan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


There's nothing about priority that says a higher priority thread will finish before the next lower thread starts.


Why we use priority then??

I used the synchronized block but still my output is not correct:


Zulfi.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uses the object instance referenced by "this" to do the synchronization. You have three instances (obj1,obj2,obj3) so the reference of "this" is different for each of them. The net result being that one doesn't block the other. You could make a single static object of any type (e.g. String) and use that in place of "this".

This still seems more complicated than handling the problem procedurally.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like you are trying to force fit your problem into threads. At this point what purpose do threads serve?
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zulfi Khan wrote:Why we use priority then??


Well, "we" don't use priority. There may be some specialized scenarios for which assigning different priorities to threads might be useful. Or more likely back when Java was young and there was only one CPU per machine, that might have been useful. Or perhaps the designers of the Thread class noticed that real-life threads in operating systems had priority levels so they decided that their Thread class needed a "priority" attribute in case anybody ever needed to use the priority levels.

But to me it's pretty clear that allowing users to set the priority of a thread is a bad thing, when the thread is running in a machine which is running a large number of tasks. Like all of the machines we've been running Java on for the last 20 years, in fact. Users are naturally going to say "My task has to run at the highest priority" and that's just going to interfere with all of the other tasks which are trying to get their job done. So Carey's test shows that the priority levels are more or less ignored, which doesn't surprise me at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!