SummerVocation_Learning--java的线程同步

 

一般来讲,我们对于“修改”的语句要上锁,对于“输出”的语句不上锁。

public class Test_XCTB implements Runnable{
	Timer timer = new Timer();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test_XCTB test = new Test_XCTB();
		Thread t1 = new Thread(test);
		Thread t2 = new Thread(test);
		t1.setName("t1");
		t2.setName("t2");
		t1.start(); t2.start();
	}
	public 	void run() {  //因为Test_XCTB实现了Runnable接口,所以要重写run方法
		timer.add(Thread.currentThread().getName());
	}
	
}

class Timer{
	private static int num = 0;
	public void add(String neme) { //或者public synchronized void add(String neme)
	synchronized(this) {
		num++;
		try {
			Thread.sleep(1);//休眠1毫秒
		} catch (InterruptedException e) {}
		
		System.out.println("This is the " + num + "th");
		
		}
	}
}

/*
 * 输出结果为:This is the 2th;This is the 2th;两个num居然一样,与期望的1th和2th不一样!
 * 原因在于在执行t1的时候,num++变成1;但遇到了sleep方法,所以t1停止,执行t2;此时num++变成2;然后在执行输出语句。
 * 但其实不加sleep语句也会遇到t1被打断而出现一样的结果。
 * 解决方法是在num语句前加上synchronized(this){}方法,表示将当前线程锁定,num也随之被锁定。
 * 还可以简便的在public void add(String neme)方法处加上synchronize,变成public synchronize void add。
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值