Java面试_多线程打印a1b2

package practice;

import java.io.IOException;

public class Main {
    static final  Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            String a[] = {"a","b"};
            @Override
            public void run() {
                for (int i = 0; i < 2; i++) {
                    synchronized (object){
                        System.out.println("xiancheng a START");
                        object.notify();
                        try {
                            System.out.println("a wait");
                            object.wait();
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        }
                        System.out.println("a 继续执行");
                        System.out.println(a[i]);
                        System.out.println("a end");
                        object.notify();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            int[] a = {1,2};
            @Override
            public void run() {
                for (int i = 0; i < 2; i++) {
                    synchronized (object){
                        System.out.println("xiancheng 1 START");
                        object.notify();
                        try {
                            System.out.println("1  wait");
                            object.wait();
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        }
                        System.out.println("1  继续执行");
                        System.out.println(a[i]);
                        System.out.println("1  end");
                        object.notify();
                    }
                }
            }
        }).start();
    }
}

 

你可能感兴趣的:(Java程序员面试笔记)