Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7995677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:29:29+00:00 2026-06-04T14:29:29+00:00

I am new to the world of java and threads..I was just going through

  • 0

I am new to the world of java and threads..I was just going through an example code as below:-

package com.alice.learnthread;

class NewThread implements Runnable{
Thread t;
long clicker=0;

private volatile boolean running=true;
NewThread(int p){
    t=new Thread(this);
    t.setPriority(p);
}
public void run(){
    while(running){
        clicker++;
    }
}
public void stop(){
    running=false;
}
public void start(){
    t.start();
}

}

 public class TestThread {
public static void main(String[] args){
    Thread r=Thread.currentThread();
    r.setPriority(Thread.MAX_PRIORITY);
    NewThread hi=new NewThread(Thread.NORM_PRIORITY+2);
    NewThread lo=new NewThread(Thread.NORM_PRIORITY-2);
    hi.start();
    lo.start();
    try{
        r.sleep(5000);
    }catch(InterruptedException e){
        System.out.println("caught");
    }
    hi.stop();
    lo.stop();
    try{
        hi.t.join();
        lo.t.join();
    }catch(InterruptedException e){
        System.out.println("cau1");
    }
    System.out.println("hi = "+hi.clicker+" lo="+lo.clicker);
}

}

However according to the output in the book the thread with high priority should have higher value for the variable clicker. But in my case the values for the variable clicker is much higher for the lower priority thread than for the higher priority one.The output is like below for me:-

hi = 2198713135 lo=2484053552

Does this not mean that lower priority thread got more CPU time than the higher priority one…Am i missing something..The results are the same(higher clicker value for the lower priority thread) on both ubuntu and win7…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:29:31+00:00Added an answer on June 4, 2026 at 2:29 pm

    As sul said, priority is more a hint than a contract to the JVM.
    In your case, your result can be explained by several theories:

    • The second thread runs faster because it takes the benefits of the compilation of the first one and is stopped after the first one.
    • The facts that the while loop check the value of a volatile variable force the jvm to actualize the value and during this time could give the other thread the CPU.
    • The stop methods takes lot of time to stop the thread.

    That is just some facts to say that how thread act is unpredictable. For example try to start the low priority thread first and I am sure you will have a different result.

    Also, try this :

    public class TestThread
    {
        public static void main(String[] args){
            Thread r=Thread.currentThread();
            r.setPriority(Thread.MAX_PRIORITY);
            NewThread hi=new NewThread(Thread.MAX_PRIORITY);
            NewThread lo=new NewThread(Thread.MIN_PRIORITY);
            hi.start();
            lo.start();
            try{
                r.sleep(5000);
            }catch(InterruptedException e){
                System.out.println("caught");
            }
            hi.interrupt();
            lo.interrupt();
    
            System.out.println("hi="+hi.clicker);
            System.out.println("lo="+lo.clicker);
        }
    }
    class NewThread extends Thread{
        long clicker=0;
    
        NewThread(int p){
            setPriority(p);
        }
        public void run(){
            while(true){
                clicker++;
            }
        }
    }
    

    I am sure that removing the volatile variable and changing how the thread is stopped will give you an other result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Java Multithreading World, I wrote this program, I just wanted
I am new to Java world but I am pretty good at using Flex,
I'm new in the Java world and I'm totally confused with the sheer number
I am pretty new to the whole Java and OSGi world and I have
I'm brand new to java, coming from a ruby world. One thing I love
I am brand new to the Java EE world. As an exercise to try
I'm new in the scala world, so excuse my trivial question. :) I just
i am new to java and i just made my first program: HelloWorld and
Assume we have a trivial Java program that consists of just one class: public
Im working on a java application that involves threads. So i just wrote a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.