-1
    public void run(){
try{for(int i=5;i>0;i--){
      System.out.println("My Thread: " +i);
      t.sleep(2000);
   }//end of for
}catch(InterruptedException e){System.out.println("MyThread Interrupted");}
  System.out.println("MyThread exiting"); }
}//end of class

Like I want to have two threads with the only difference being t.sleep(1000) and t1.sleep(2000). How can I do that???

Jazz B
  • 13
  • 3
  • 2
    On how to pass a parameter to a thread: http://stackoverflow.com/questions/877096/how-can-i-pass-a-parameter-to-a-java-thread – Egg Apr 18 '14 at 20:40
  • FYI, `sleep` is a **static** method. When you say `t.sleep(2000)`, it doesn't matter what `t` is (it could even be `null`). The compiler treats it just like `Thread.sleep(2000)`, which causes the current thread to sleep. – ajb Apr 18 '14 at 20:41

1 Answers1

2

The class should take an additional constructor for sleepTime.

public class MyRunnable implements Runnable() {
    private final long sleepTime;

    public MyRunnable(long sleepTime) {
        this.sleepTime = sleepTime;
    }

    @Override public void run() {
        // ...
        Thread.sleep(sleepTime);
    }
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Should I implement it like this? 'MyRunnable t1=new MyRunnable(1000);MyRunnable t2=new MyRunnable(2000);t1.start();t2.start()' – Jazz B Apr 18 '14 at 20:56