单线程代码事例

单线程代码事例

package day12;

public class SynchronizedDemo3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ResTest r = new ResTest();
		InputTest inputTest = new InputTest(r);
		Thread  t = new Thread(inputTest);
		t.start();
	}

}

class ResTest{
	String name;
	String sex ;
	boolean flag = false;
}

class InputTest implements Runnable{
    private ResTest r;
    public InputTest(ResTest r){
    	this.r = r;
    }
	@Override
	public void run() {
		// TODO Auto-generated method stub
		int x = 0;
		while(true){
			if(x == 0){
				r.name = "mike";
				r.sex ="man";
				System.out.println(r.name +"--"+r.sex);
			}else{
				r.name = "小丽";
				r.sex ="女-------";
				System.out.println(r.name +"--"+r.sex);
			}
			x = (x+1)%2;
			
		}
	}
	
}

  

原文地址:https://www.cnblogs.com/childhooding/p/4742604.html