共享内存 Java

package thread;

public class MyThread implements Runnable {
	private int i;

	public MyThread() {
		i = 0;
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				System.out.println("Error Sleep");
			}
			synchronized (this) {
				System.out.println(Thread.currentThread().getName() + " : "
						+ i++);
			}
		}
	}

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		new Thread(mt, "Thread 0").start();
		new Thread(mt, "Thread 1").start();
	}

}

你可能感兴趣的:(共享内存 Java)