import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockTest {
public final static Lock varLcok=new ReentrantLock();
public void Add(){
varLcok.lock();
System.out.println("add");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
varLcok.unlock();
}
public void Mul(){
varLcok.lock();
System.out.println("mul");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
varLcok.unlock();
}
public void EXE() {
new Thread(){
@Override
public void run() {
while (true) {
Add();
}
}
}.start();
new Thread(){
@Override
public void run() {
while (true) {
Mul();
}
}
}.start();
}
}
JAVA的线程锁
最新推荐文章于 2022-04-15 09:22:04 发布