Results 1 to 7 of 7

Thread: Write A program Code In Java To create threads of different priority

  1. #1

    Write A program Code In Java To create threads of different priority

    i went through one blog of this site on thread priority. thanks for such information. but now i need help from you people also for my code. please help asap.
    techforum4u.com Member!!

  2. #2
    Super Moderator angad's Avatar
    Join Date
    Aug 2010
    Location
    nature
    Posts
    505
    Blog Entries
    140
    Rep Power
    4

    Thumbs up Re: Write A program Code In Java To create threads of different priority

    well you could refer this code. if this satisfies your query then let me know
    Code:
    /*   Create four threads with priority 1,3,5,7 
    respectively. Update a counter in each of the 
    threads for ten sec. print the final value 
    of the count from each thread. */ 
    
    class ThreadPrio implements Runnable
    {
    	public long count=0;
        Thread t;
        private volatile boolean running=true;
        
        public ThreadPrio(int p)
        {
        	t=new Thread(this);
        	t.setPriority(p);
        }
        public void run()
        {
        	while(running)
        	    count++;
        }
        public void start()
        {
        	t.start();
        }
        public void stop()
        {
        	running=false;
        	
        }
    
    public static void main(String[] args) throws InterruptedException
    {
    	Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    	ThreadPrio t7=new ThreadPrio(7);
    	ThreadPrio t5=new ThreadPrio(5);
    	ThreadPrio t3=new ThreadPrio(3);
    	ThreadPrio t1=new ThreadPrio(1);
        t1.start();
        t3.start();
        t5.start();
        t7.start();
        System.out.println("Main going to sleep for 10 secs. Plz wait patiently.");
        Thread.sleep(10000);
        t1.stop();
        t3.stop();
        t5.stop();
        t7.stop();
        
        t1.t.join();
        t3.t.join();
        t5.t.join();
        t7.t.join();
        
        System.out.println("Thread with Priority 1 :"+t1.count);
        System.out.println("Thread with Priority 3 :"+t3.count);
        System.out.println("Thread with Priority 5 :"+t5.count);
        System.out.println("Thread with Priority 7 :"+t7.count);
        
    
    }     
    
    
    
    }
    techforum4u.com Member!!

  3. #3

    Re: Write A program Code In Java To create threads of different priority

    good work angad sir and ya thanks to sachdev also for putting this question before me beacuse i was going to ask this.
    techforum4u.com Member!!

  4. #4

    Re: Write A program Code In Java To create threads of different priority

    thaks tf4u for providing such solutions and helping us.
    techforum4u.com Member!!

  5. #5

    Re: Write A program Code In Java To create threads of different priority

    good work angad. liked the program.
    techforum4u.com Member!!

  6. #6

    Re: Write A program Code In Java To create threads of different priority

    he following example demonstrates two threads at different priorities, which do not run on a preemptive platform in the same way as they run on a nonpreemptive platform. One thread is set two levels above the normal priority, as defined by Thread.NORM_PRIORITY, and the other is set to two levels below it. The threads are started and allowed to run for ten seconds. Each thread executes a loop, counting the number of iterations. After ten seconds, the main thread stops both threads. The number of times that each thread made it through the loop is then displayed.
    Code:
    // Demonstrate thread priorities.
    class clicker implements Runnable {
    int click = 0;
    Thread t;
    private volatile boolean running = true;
    public clicker(int p) {
    t = new Thread(this);
    t.setPriority(p);
    }
    public void run() {
    while (running) {
    click++;
    }
    }
    public void stop() {
    running = false;
    }
    public void start() {
    t.start();
    }
    }
    
    class HiLoPri {
    public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
    clicker lo = new clicker(Thread.NORM_PRIORITY - 2);
    lo.start();
    hi.start();
    try {
    Thread.sleep(10000);
    } catch (InterruptedException e) {
    System.out.println("Main thread interrupted.");
    }
    lo.stop();
    hi.stop();
    // Wait for child threads to terminate.
    try {
    hi.t.join();
    lo.t.join();
    } catch (InterruptedException e) {
    System.out.println("InterruptedException caught");
    }
    System.out.println("Low-priority thread: " + lo.click);
    System.out.println("High-priority thread: " + hi.click);
    }
    }
    The output of this program, shown as follows when run under Windows 98, indicates that the threads did context switch, even though neither voluntarily yielded the CPU nor blocked for I/O. The higher-priority thread got approximately 90 percent of the CPU time.

    Low-priority thread: 4408112
    High-priority thread: 589626904

    Of course, the exact output produced by this program depends on the speed of your CPU and the number of other tasks running in the system. When this same program is run under a nonpreemptive system, different results will be obtained.

    One other note about the preceding program. Notice that running is preceded by the keyword volatile.Volatile is used here to ensure that the value of running is examined each time the following loop iterates:

    while (running) {
    click++;
    }

    Without the use of volatile, Java is free to optimize the loop in such a way that the value of running is held in a register of the CPU and not necessarily reexamined with each iteration. The use of volatile prevents this optimization, telling Java that running may change in ways not directly apparent in the immediate code.
    Last edited by Harsh; 08-08-2011 at 01:10 PM.

  7. #7

    Re: Write A program Code In Java To create threads of different priority

    Thread.MIN_PRIORITY -The maximum priority of any thread (an int value of 10)
    Thread.MAX_PRIORITY -The minimum priority of any thread (an int value of 1)
    Thread.NORM_PRIORITY - The normal priority of any thread (an int value of 5)
    setPriority() - This is method is used to set the priority of thread.
    getPriority() - This method is used to get the priority of thread.

Similar Threads

  1. Replies: 0
    Last Post: 11-03-2011, 06:20 PM
  2. Replies: 7
    Last Post: 08-06-2011, 09:41 AM
  3. Replies: 1
    Last Post: 02-27-2011, 08:34 AM
  4. Replies: 3
    Last Post: 02-21-2011, 08:50 AM
  5. Replies: 8
    Last Post: 02-20-2011, 06:13 AM

Members who have read this thread : 3

You do not have permission to view the list of names.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.