Reputation: 15
this may be a very silly question but I tried setting priority for a few threads in a java program but it seems like the order in which I start each thread only determines which one is more favored.
I am trying to run 4 Tasks and increment 4 separate counters for each Task. Then I am putting the main thread to sleep for 10 sec while the 4 Threads run and increment their counters.Once the Main thread is revived the program terminates after printing the final count values.
What I got as output was something like this:
Job 1: 5876
Job 2: 6546
Job 3: 6020
Job 4: 0
Even though the thread running job 4 got highest priority.
I think I have some serious conceptual problem here...And maybe what I am trying to do is not what I am doing in the program. Please help me. Thank you in Advance.
class Task1 implements Runnable
{
public int count=0;
public void run()
{
while(true)
count++;
}
}
class Threadcount
{
public static void main(String[] args)
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Task1 job1=new Task1();
Task1 job2=new Task1();
Task1 job3=new Task1();
Task1 job4=new Task1();
Thread t1=new Thread(job1);
Thread t2=new Thread(job2);
Thread t3=new Thread(job3);
Thread t4=new Thread(job4);
t1.setPriority(1);
t2.setPriority(3);
t3.setPriority(5);
t4.setPriority(7);
try
{
Thread.sleep(10000);
}
catch(Exception e)
{
}
t1.start();
t2.start();
t3.start();
t4.start();
System.out.println("Job 1: "+job1.count);
System.out.println("Job 2: "+job2.count);
System.out.println("Job 3: "+job3.count);
System.out.println("Job 4: "+job4.count);
System.exit(1);
}
}
Upvotes: 0
Views: 1464
Reputation: 2322
The Java API states:
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.
However, if thread priorities will be taken into consideration depends on the scheduling algorithm which might differ between operating systems, versions of the same operating system, or different JVMs. Thereby it is considered a bad practice to rely on thread priorities for your java program's correctness, as this could lead to unexpected behaviour if run on different OS or different JVM.
Upvotes: 0
Reputation: 5639
Better use a lock inside the task were all Threads must wait for a start signal. Otherwise you cant ensure they start parallel. Here is a simple soulution using a CountDownLatch which ensures that all tas start at the same time.
public class Threadcount {
static CountDownLatch s = new CountDownLatch(4);
static volatile boolean run=true;
static class Task1 implements Runnable {
public long count = 0;
public void run() {
s.countDown();
try {
s.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
while (run)
count++;
}
}
public static void main(String[] args) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Task1 job1 = new Task1();
Task1 job2 = new Task1();
Task1 job3 = new Task1();
Task1 job4 = new Task1();
Thread t1 = new Thread(job1);
Thread t2 = new Thread(job2);
Thread t3 = new Thread(job3);
Thread t4 = new Thread(job4);
t1.setPriority(1);
t2.setPriority(3);
t3.setPriority(5);
t4.setPriority(7);
t1.start();
t2.start();
t3.start();
t4.start();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
run = false;
System.out.println("Job 1: " + job1.count);
System.out.println("Job 2: " + job2.count);
System.out.println("Job 3: " + job3.count);
System.out.println("Job 4: " + job4.count);
}
Upvotes: 1
Reputation: 726
Your threads execute for a very short span of time. If you execute them longer, there is a chance that you might see more count in higher priority thread. Even then main thing is that there is no guarantee by JVM that this difference in count will be visible in the time your program runs. If your functionality depends on one thread to finish before other, explore Thread join . However, in your particular case join won't work because Threads are running in infinite loop
Upvotes: 0
Reputation: 691715
Your benchmark has several big problems.
Upvotes: 3