首页 > 试题广场 >

写2个线程,其中一个线程打印1~52,另一个线程打印A~Z,

[问答题]
写2个线程,其中一个线程打印1~52,另一个线程打印A~Z,打印顺序应该是12A34B56C...5152Z。
public class Main {

    public static void main(String[] args) {

        final Object obj = new Object();
        Thread a = new Thread(()->{
            for(char i = 'A'; i <= 'Z'; ++i ) {
                synchronized (obj) {
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print(i);
                    obj.notify();
                }
            }
        });

        Thread b = new Thread(()->{
           for(int i = 1;  i <= 52; i += 2) {
               synchronized (obj) {
                   System.out.print(i);
                   System.out.print(i+1);
                   obj.notify();
                   try {
                       obj.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
        });
        a.start();
        b.start();
    }
}
编辑于 2019-06-25 11:08:20 回复(0)
public class ThreadPractice1 {
    public static void main(String[] args) throws InterruptedException {
        Object obj = new Object();
        Runnable r1 = new Runnable() {
            private int i = 1;

            @Override
            public void run() {

                synchronized (obj) {
                    while (i <= 52) {
                        System.out.print(i);
                        ++i;
                        System.out.print(i);
                        ++i;
                        obj.notify();
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }


            }
        };
        Runnable r2 = new Runnable() {
            private char c = 'A';

            @Override
            public void run() {

                synchronized (obj) {
                    while (c <= 'Z') {
                        System.out.print(c);
                        c++;
                        obj.notify();
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }

            }
        };
        Thread t1 = new Thread(r1);

        Thread t2 = new Thread(r2);
        t1.start();
        TimeUnit.MILLISECONDS.sleep(200); //保证线程1先执行
        t2.start();
    }
}
发表于 2018-09-13 10:54:09 回复(0)
wait和notify线程通讯
发表于 2018-09-12 23:36:41 回复(0)
public class Test001 {
    public static void main(String[] args) {
        Object obj = new Object();
        new Thread(() -> {

            synchronized (obj) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i = 1;
                while (i <= 52) {
                    System.out.print(i);
                    if (i % 2 == 0) {
                        obj.notify();
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    i++;
                }
            }

            try {
                obj.wait();
                for (int i = 1; i <= 52; i++) {
                    if (i % 2 == 0) {
                        obj.notify();
                        obj.wait();
                    }
                    System.out.print(i);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            synchronized (obj) {
                char c = 'A';
                while (c <= 'Z') {
                    obj.notify();
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print(c++);
                }
            }
        }).start();
    }
} 

编辑于 2018-12-17 13:30:32 回复(0)
/**
 * 写2个线程,其中一个线程打印1~52,另一个线程打印A~Z,打印顺序应该是12A34B56C...5152Z。
 * @author Dx
 *
 */
public class Demo {
    public static void main(String[] args) {
        Object obj=new Object();
        Runnable r1=new Runnable() {
            int num=52;
            int i=1;
            public void run() {
                synchronized (obj) {
                    while(i<=num){
                        obj.notify();
                        System.out.print(i);
                        if(i%2==0){
                            try {
                                obj.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        i++;
                    }
                }
            }
        };
        Runnable r2=new Runnable() {
            char a='A';
            @Override
            public void run() {
                synchronized (obj) {
                    while(a<='Z'){
                        obj.notify();
                        System.out.print(a);
                        a++;
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        Thread t1=new Thread(r1);
        Thread t2=new Thread(r2);
        t1.start();
        t2.start();
    }
}
发表于 2019-01-30 23:44:15 回复(0)
线程不是为了让两件事分开 互不干扰吗 为什么还要规定输出顺序
发表于 2018-09-14 11:54:36 回复(1)

可以考略wait,notify的线程通信方式


发表于 2018-09-11 22:04:23 回复(0)