threadcondemo.java

来自「JAVA编程思想源代码 值得一下 很难找的」· Java 代码 · 共 25 行

JAVA
25
字号
package chapter13;

public class ThreadConDemo implements Runnable {

	private int n = 10; // 线程共享变量

	public void run() {

		for (int i = 0; i < 5; i++) {
			n -= i; // 吃了i个苹果
			Thread.yield(); // 当前线程暂时让出CPU给其他线程,让其他线程运行
			n += i; // 补上i个苹果
			System.out.println(n);
		}
	}

	public static void main(String args[]) throws Exception {
		ThreadConDemo apple = new ThreadConDemo();
		Thread thread1 = new Thread(apple);
		Thread thread2 = new Thread(apple);
		thread1.start();
		thread2.start();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?