⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testthread4.java.bak

📁 文件输入流
💻 BAK
字号:
class MyThread implements Runnable{

	volatile private static int share;
	public MyThread(int _share){
		share=_share;
	}
	
	/**
			synchronized静态方法的形式,解决多线程同步的问题
			synchronized 静态方法:监视器就是相应的类
	*/
	synchronized public static void setI(){
		while(true){
			if (share > 0) {
				try{
						Thread.sleep(200);	
				}
				catch(Exception e){
						System.out.println("捕获到异常");	
				}
				share--;	
				System.out.println(Thread.currentThread().getName()+"  share = " + share);
			} else {
				System.out.println(Thread.currentThread().getName()+"  share <= 0");
				break;
			}
		}
	}
	 public void run() {
	 		MyThread.setI();
	}

}
public class TestThread4 {
	public static void main(String[] args) {
		MyThread mt=new MyThread(20);
		MyThread mt2=mt;
		Thread t1=new Thread(mt);
		Thread t2=new Thread(mt2);
		t1.start();
		t2.start();
	}

}

⌨️ 快捷键说明

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