创建两个线程,一个显示奇数和其他偶数

2022-09-02 00:04:02

我正在尝试创建两个线程,一个线程显示从0到10的偶数整数,一个线程显示从1到11的奇数。以下代码是否适合设计此程序?

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);
        }
    }
}

答案 1

@aymeric答案不会按自然顺序打印数字,但此代码会。最后的解释。

public class Driver {
    static Object lock = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            public void run() {

                for (int itr = 1; itr < 51; itr = itr + 2) {
                    synchronized (lock) {
                        System.out.print(" " + itr);
                        try {
                            lock.notify();
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            public void run() {

                for (int itr = 2; itr < 51; itr = itr + 2) {
                    synchronized (lock) {
                        System.out.print(" " + itr);
                        try {
                            lock.notify();
                            if(itr==50)
                                break;
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        try {
            t1.start();
            t2.start();
            t1.join();
            t2.join();
            System.out.println("\nPrinting over");
        } catch (Exception e) {

        }
    }
}

为了实现这一点,上面两个线程的运行方法必须一个接一个地调用,即它们需要同步,我正在使用锁来实现这一点。

代码的工作方式如下:t1.run 打印奇数并通知任何等待线程它将释放锁,然后进入等待状态。

此时调用 t2.run,它将打印下一个偶数,通知其他线程它将释放它所持有的锁,然后进入等待状态。

这种情况一直持续到 t2.run() 中的 itr 达到 50,此时我们的目标已经实现,我们需要杀死这两个线程。

通过中断,我避免在t2.run中调用lock.wait(),因此t2线程被关闭,控件现在将转到t1.run,因为它正在等待获取锁;但是这里的itr值将>51,我们将从它的run()中出来,从而关闭线程。

如果在 t2.run() 中未使用 break,尽管我们将在屏幕上看到数字 1 到 50,但这两个线程将进入死锁状态并继续处于等待状态。


答案 2

我只会改变一些细节(这里不需要使用模运算符...):

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);
        }
    }
}