编写两个线程,顺序输出自然顺序:1,2,3,4,...,99,100

并发编程。请编写两个线程,线程1顺序输出1,3,5,...,99等奇数,每个数一行。线程2顺序输出2,4,6,...,100等偶数,每个数一行。最终的结果要求是输出为自然顺序:1,2,3,4,...,99,100。

 方法一:

创建线程类RunnableDemo1.Java

public class RunnableDemo1 implements Runnable{

    private Thread t;
    private String threadName;
    private Object curlock;

    RunnableDemo1( String name,Object lock) {
        super();
        threadName = name;
        curlock = lock;
    }

    public void run() {
        synchronized (curlock) {
            try {
                for (int i = 1; i < 100; ) {
                    System.out.println(i);
                    i = i + 2;
                    curlock.notifyAll();
                    curlock.wait();
                }
            } catch (InterruptedException e) {
                System.out.println("Thread " + threadName + " interrupted.");
            }
        }
    }
    public void start () {
//        System.out.println("Starting " +  threadName );
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }

    public void interrupt () {
        if (t == null) {
            t = new Thread (this, threadName);
            t.interrupt ();
        }
    }
}

  创建线程类RunnableDemo2.Java

public class RunnableDemo2 implements Runnable{

    private Thread t;
    private String threadName;
    private Object curlock ;

    RunnableDemo2( String name,Object lock) {
        super();
        threadName = name;
        curlock = lock;
    }

    public void run() {
        synchronized (curlock) {
            try {
                for(int i = 2; i <= 100; ) {
                    System.out.println(i);
                    i=i+2;
                    curlock.notifyAll();
                    curlock.wait();
                }
            }catch (InterruptedException e) {
                System.out.println("Thread " +  threadName + " interrupted.");
            }
        }
    }

    public void start () {
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }

    public void interrupt () {
        if (t == null) {
            t = new Thread (this, threadName);
            t.interrupt ();
        }
    }
}

  测试类代码:

public class Test {

    public static void main(String args[]) throws Exception{
        final Object lock = new Object();
        RunnableDemo1 R1 = new RunnableDemo1( "Thread-1",lock);
        R1.start();
        Thread.sleep(100);
        RunnableDemo2 R2 = new RunnableDemo2( "Thread-2",lock);
        R2.start();
        R1.interrupt();
        R2.interrupt();
    }
}

 方法二:

public class ThreadTest implements Runnable{
    int i = 1;
    public void run() {
        while (true) {
            /*ThreadTest,因为使用的是implements方式。若使用继承Thread类的方式,慎用this*/
            synchronized (this) {
                /*唤醒另外一个线程,注意是this的方法,而不是Thread*/
                notify();
                try {
                    /*使其休眠100毫秒,放大线程差异*/
                    Thread.currentThread();
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i <= 100) {
                    System.out.println(i);
                    i++;
                    try {
                        /*放弃资源,等待*/
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

  测试方法如下:

public static void main(String[] args) {

        ThreadTest t = new ThreadTest();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        t1.start();
        t2.start();

    }

  

posted @ 2019-10-21 16:18  loytime  阅读(636)  评论(0编辑  收藏  举报