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:28
public class ThreadExample {

Object lock = new Object();

class ThreadEven implements Runnable {

    @Override
    public void run() {
        int i = 2;
        while (i <= 20) {
            synchronized (lock) {
                System.out.println(i + " ");
                i = i + 2;
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

class ThreadOdd implements Runnable {

    @Override
    public void run() {
        int i = 1;
        while (i <= 20) {
            synchronized (lock) {
                System.out.println(i + " ");
                i = i + 2;
                try {
                    lock.notify();
                    lock.wait();
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }
        }

    }

}

public static void main(String args[]) {

    ThreadExample example = new ThreadExample();
    ThreadExample.ThreadOdd odd = example.new ThreadOdd();
    ThreadExample.ThreadEven even = example.new ThreadEven();

    Thread oT = new Thread(odd);
    Thread eT = new Thread(even);

    oT.start();
    eT.start();

}
查看更多
做个烂人
3楼-- · 2020-02-08 06:28

package p.Threads;

public class PrintEvenAndOddNum  {

    private  Object obj = new Object();

    private static final PrintEvenAndOddNum peon = new PrintEvenAndOddNum();

    private PrintEvenAndOddNum(){}

    public static PrintEvenAndOddNum getInstance(){
        return peon;
    }

    public  void printOddNum()  {
        for(int i=1;i<10;i++){
            if(i%2 != 0){
                synchronized (obj) {
                    System.out.println(i);

                    try {
                        System.out.println("oddNum going into waiting state ....");
                        obj.wait();

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("resume....");
                    obj.notify();
                }
            }
        }
    }

    public  void printEvenNum()  {
        for(int i=1;i<11;i++){
            if(i%2 == 0){
                synchronized(obj){
                    System.out.println(i);
                    obj.notify();
                    try {
                        System.out.println("evenNum going into waiting state ....");
                        obj.wait();
                        System.out.println("Notifying waiting thread ....");
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }   
}
查看更多
Juvenile、少年°
4楼-- · 2020-02-08 06:29

I would just change a few details (no need to use the modulo operator here...):

public class Mythread {

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

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

class Runnable1 implements Runnable{
    public void run(){
        for(int i=1;i<=11;i+=2) {
           System.out.println(i);
        }
    }
}
查看更多
神经病院院长
5楼-- · 2020-02-08 06:29

I would also look at using Java Concurrency if you want alternatives. Some of the functionality provided in the Java Concurrency package offer a higher level of abstraction then using the Thread class directly and provide more functionality as a result.

For your specific case what your doing is quite reasonable however is the order of the printing of these numbers important? Do you want evens before odds? These kinds of questions would better indicate the design that most suits your needs.

查看更多
闹够了就滚
6楼-- · 2020-02-08 06:34
public class MyThread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Threado o =new Threado();
        o.start();

        Threade e=new Threade();
        e.start();    
    }    
}


class Threade extends Thread{

    public void run(){

        for(int i=2;i<10;i=i+2)
            System.out.println("evens "+i);         
    }       
}


class Threado extends Thread{

    public void run(){

        for(int i=1;i<10;i=i+2)
            System.out.println("odds "+i);          
    }       
}

OUTPUT :-

odds 1 odds 3 odds 5 odds 7 odds 9 evens 2 evens 4 evens 6 evens 8

查看更多
戒情不戒烟
7楼-- · 2020-02-08 06:37

Yes it is fine. But in this case, I don't think you need 2 threads are all, because the operation is simple. However, if you are practicing threads, it is OK

查看更多
登录 后发表回答