线程安全问题(锁机制,同步技术)
作者:
奇怪的双子座 | 来源:发表于
2019-08-07 12:55 被阅读0次package com.lqf;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SyncRunableImpl implements Runnable{
private int ticker=100;
Lock lock=new ReentrantLock();
//创建一个锁对象
Object obj=new Object();
// @Override
// public void run(){
// while (ticker>0){
// synchronized (obj){
// try {
// Thread.sleep(1000);
// }catch (InterruptedException e){
// e.getMessage();
// }
// System.out.println(ticker+"ticker");
// ticker--;
// }
//
// }
//
// }
@Override
public void run(){
while (ticker>0){
lock.lock();
try {
Thread.sleep(1000);
System.out.println(ticker+"ticker");
ticker--;
}catch (InterruptedException e){
e.getMessage();
}finally {
lock.unlock();
}
}
}
}
本文标题:线程安全问题(锁机制,同步技术)
本文链接:https://www.haomeiwen.com/subject/mpwndctx.html
网友评论