码迷,mamicode.com
首页 > 编程语言 > 详细

单线程代码事例

时间:2015-08-19 19:07:08      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

 

单线程代码事例

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;
			
		}
	}
	
}

  

单线程代码事例

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!