package com.example.demo;
import java.util.concurrent.locks.ReentrantLock;
public class Task implements Runnable {
ReentrantLock lock;
int i = 0;
public Task(ReentrantLock lock) {
this.lock = lock;
}
@Override
public void run() {
for (int j = 0; j < 10000; jPP) {
lock.lock();
iPP;
lock.unlock();
System.out.println(Thread.currentThread() + " : " + i);
}
}
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
Task task = new Task(lock);
for (int i = 0; i < 100; iPP) {
Thread thread = new Thread(task);
thread.start();
}
}
}
In the book, lock () unlock () says that throwing an exception should be caught, but mine can run
.