Results 1 to 4 of 4
  1. #1
    260191894 is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Problem with setting priorities in threads

    Hi everyone! I am practicing an exercise in a book. I am learning how to set priorities to threads. Here is my code. I am trying to make thread 3 finish first before threads 1 and 2 but when I run the program, it doesn't work. Could someone please help?

    public class TaskThreadDemo {
    public static void main(String[ ] args){
    Runnable printA = new PrintChar('a',100);
    Runnable printB = new PrintChar('b',100);
    Runnable print100 = new PrintNum(100);

    Thread thread1 = new Thread(printA);
    Thread thread2 = new Thread(printB);
    Thread thread3 = new Thread(print100);

    thread1.start();
    thread2.start();
    thread3.start();
    thread3.setPriority(Thread.MAX_PRIORITY);
    }
    }

    class PrintChar implements Runnable{
    private char charToPrint;
    private int times;

    public PrintChar(char c, int t){
    charToPrint = c;
    times = t;
    }

    public void run(){
    for(int i = 0; i < times; i++){
    System.out.print(charToPrint);
    }
    }
    }

    class PrintNum implements Runnable{
    private int lastNum;

    public PrintNum(int n){
    lastNum = n;
    }

    public void run(){
    for(int i = 1; i <= lastNum; i++){
    System.out.print(" " + i);
    }
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Problem with setting priorities in threads

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    chitd is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Re: Problem with setting priorities in threads

    I think you should use join method of thread. You can write as below
    ....
    thread3.start();
    thread3.join();
    thread1.start();
    thread2.start();
    ...
    let try again.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Problem with setting priorities in threads

    chitd, the question was asked nearly 3 months ago. If you want to be a contributing member of these forums, please stay withe the current forum threads and don't needlessly bump threads in which the questioner has lost interest and abandoned.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 03-19-2012, 05:09 PM
  2. Java Thread Priorities
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-16-2009, 08:16 PM
  3. Problem in setting Eclipse
    By Crisanto in forum New To Java
    Replies: 3
    Last Post: 10-29-2008, 06:48 PM
  4. How to use thread priorities
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:35 PM

Posting Permissions

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