Create two threads, one display odd & other even n

2020-02-08 05:57发布

I'm trying to create two threads, one thread display even integers from 0 to 10, one thread display odd integers from 1 to 11. Is the following code suitable for designing this program?

public class Mythread {

    public static void main(String[] args) {
        Runnable r = new Runnable1();
        Thread t = new Thread(r);
        t.start();
        Runnable r2 = new Runnable2();
        Thread t2 = new Thread(r2);
        t2.start();
    }
}

class Runnable2 implements Runnable{
    public void run(){
        for(int i=0;i<11;i++){
            if(i%2 == 1)
                System.out.println(i);
        }
    }
}

class Runnable1 implements Runnable{
    public void run(){
        for(int i=0;i<11;i++){
            if(i%2 == 0)
                System.out.println(i);
        }
    }
}

20条回答
相关推荐>>
2楼-- · 2020-02-08 06:51
public class OddEvenPrinetr {
    private static Object printOdd = new Object();

    public static void main(String[] args) {

        Runnable oddPrinter =  new Runnable() {
            int count = 1;
            @Override
            public void run() {
                while(true){
                    synchronized (printOdd) {
                        if(count >= 101){
                            printOdd.notify();
                            return;
                        }
                        System.out.println(count);
                        count = count + 2;                                              
                        try {
                            printOdd.notify();
                            printOdd.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };


        Runnable evenPrinter =  new Runnable() {
            int count = 0;
            @Override
            public void run() {
                while(true){
                    synchronized (printOdd) {
                        printOdd.notify();
                        if(count >= 100){                       
                            return;
                        }                                       
                        count = count + 2;
                        System.out.println(count);
                        printOdd.notify();
                        try {
                            printOdd.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };

        new Thread(oddPrinter).start();
        new Thread(evenPrinter).start();
    }
}
查看更多
趁早两清
3楼-- · 2020-02-08 06:51
 public class ThreadClass {
   volatile int i = 1;
    volatile boolean state=true;

    synchronized public void printOddNumbers(){

                   try {
                       while (!state) {

                           wait();
                       }

                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                System.out.println(Thread.currentThread().getName()+" "+i);
                      state = false;
                      i++;
                      notifyAll();

    }

  synchronized  public void printEvenNumbers(){

      try {
          while (state) {

              wait();
          }

      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      System.out.println(Thread.currentThread().getName()+" "+i);
      state = true;
      i++;
      notifyAll();

    }
}

Then call the above class like this

   // I am ttying to print 10 values.
  ThreadClass threadClass=new ThreadClass();

        Thread t1=new Thread(){
            int k=0;
            @Override
            public void run() {
                while (k<5) {
                    threadClass.printOddNumbers();
                    k++;
                }
            }
        };
        t1.setName("Thread1");
        Thread t2=new Thread(){
            int j=0;
            @Override
            public void run() {
                while (j<5) {
                    threadClass.printEvenNumbers();
                    j++;
                }
            }
        };
        t2.setName("Thread2");

        t1.start();

        t2.start();
  1. Here I am trying to printing the 1 to 10 numbers.
  2. One thread trying to print the even numbers and another Thread Odd numbers.
  3. my logic is print the even number after odd number. For this even numbers thread should wait until notify from the odd numbers method.
  4. Each thread calls particular method 5 times because I am trying to print 10 values only.

out put:

System.out: Thread1 1
System.out: Thread2 2
System.out: Thread1 3
System.out: Thread2 4
System.out: Thread1 5
System.out: Thread2 6
System.out: Thread1 7
System.out: Thread2 8
System.out: Thread1 9
System.out: Thread2 10
查看更多
登录 后发表回答