在需要线程同步的时候如何选择合适的线程锁?
例:选择可以存入到常量池当中的对象,string对象等
}
public class synctest
{
// 特殊的instance变量,用于充当同步锁的对象
private byte[] lock = new byte[0];
public void method(string flag)
{
synchronized (lock)
{
system.out.println(flag + ", invoke method f....");
try
{
thread.sleep(1000);
}
catch (interruptedexception e)
{
e.printstacktrace();
}
}
}
public static void main(string[] args)
{
synctest test1 = new synctest();
synctest test2 = new synctest();
mythread1 mythread1 = new mythread1();
mythread1 mythread2 = new mythread1();
mythread1.synctest = test1;
mythread2.synctest = test1;
mythread1 mythread3 = new mythread1();
mythread1 mythread4 = new mythread1();
mythread3.synctest = test2;
mythread4.synctest = test2;
mythread1.start();
mythread2.start();
mythread3.start();
mythread4.start();
}
}
如对本文有疑问, 点击进行留言回复!!
利用python将Mysql信息以Excel文件并作为邮件附件发送
springmvc+mybaits+mysql上传表情Incorrect string value: ‘\xF0\x9F\xA4\xB4\xF0\x9F...‘ for
SpringCloud Greenwich集成Seata1.2.0详解说明(原创by ulwfcyvi)
mybatis generator生成代码库 与指定的库不一致 为其他库的同名表
网友评论