test.java

来自「学了2个月java 练习用~ 可以看下 希望同意」· Java 代码 · 共 63 行

JAVA
63
字号
public class test
{
	public static void main(String[] args)
	{
		testobject a=new testobject();
		testThreadone testone=new testThreadone(a);
		testThreadtwo testtwo=new testThreadtwo(a);
		testone.start();
		testtwo.start();
	}
}

class testobject{
	private int i=0;

	public void seti(int i){
		this.i=i;
	}

	public int geti(){
		return i;
	}
}

class testThreadone extends Thread{
	testobject now;
	public testThreadone(testobject now){
		this.now=now;
	}

	public void run(){
		try{
			if(now.geti()==0) sleep(1000);
		}
		catch (Exception e){
		}
		synchronized(now){
			for (int i=1;i<=100 ;i++ ){
				now.seti(now.geti()+i);
			}
			now.notifyAll();
		}
	}
}

class testThreadtwo extends Thread{
	testobject now;
	public testThreadtwo (testobject now){
		this.now=now;
	}

	public void run(){
		synchronized(now){
			now.seti(10000);
			try{
				now.wait();
				System.out.println(now.geti());
			}
			catch (Exception e){
			}
		}
	}
}

⌨️ 快捷键说明

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