Java : ReentrantLock 不起作用它仍然相互混合
Java : ReentrantLock doesn't work It still mixing with each other
我不明白为什么代码不能正常工作。线程仍然相互混合。
我有class个答:
package com.company;
public class A implements Runnable {
ReentrantLockClass r;
public A(ReentrantLockClass r) {
this.r = r;
}
@Override
public void run() {
r.print(5);
}
}
和class乙:
package com.company;
public class B implements Runnable{
ReentrantLockClass r;
public B(ReentrantLockClass r) {
this.r = r;
}
@Override
public void run() {
r.print(10);
}
}
这是 运行 ReentrantLock
的 ReentrantLockClass
package com.company;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockClass {
public void print(int count){
ReentrantLock lock = new ReentrantLock();
try {
lock.lock();
for(int i = 1 ; i <= 10 ; i ++){
System.out.println(count * i);
Thread.sleep(500);
}
}
catch (Exception e){}
finally {
lock.unlock();
}
}
}
这是主要的class:
package com.company;
public class Main {
public static void main(String[] args) {
ReentrantLockClass r = new ReentrantLockClass();
A a = new A(r);
B b = new B(r);
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}
}
这是输出:
它应该 运行 一个线程然后 运行 其他线程
print
方法创建一个新的 ReentrantLock
实例。因此,每个线程都锁定自己的锁,因此它们不会相互阻塞。一种方法是在 ReentrantLockClass
'(隐式)构造函数中创建锁,然后在 print
方法中锁定它:
public class ReentrantLockClass {
// One lock shared for the instance
ReentrantLock lock = new ReentrantLock();
public void print(int count){
try {
lock.lock();
for(int i = 1 ; i <= 10 ; i ++){
System.out.println(count * i);
Thread.sleep(500);
}
}
catch (Exception e){}
finally {
lock.unlock();
}
}
}
我不明白为什么代码不能正常工作。线程仍然相互混合。
我有class个答:
package com.company;
public class A implements Runnable {
ReentrantLockClass r;
public A(ReentrantLockClass r) {
this.r = r;
}
@Override
public void run() {
r.print(5);
}
}
和class乙:
package com.company;
public class B implements Runnable{
ReentrantLockClass r;
public B(ReentrantLockClass r) {
this.r = r;
}
@Override
public void run() {
r.print(10);
}
}
这是 运行 ReentrantLock
的 ReentrantLockClasspackage com.company;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockClass {
public void print(int count){
ReentrantLock lock = new ReentrantLock();
try {
lock.lock();
for(int i = 1 ; i <= 10 ; i ++){
System.out.println(count * i);
Thread.sleep(500);
}
}
catch (Exception e){}
finally {
lock.unlock();
}
}
}
这是主要的class:
package com.company;
public class Main {
public static void main(String[] args) {
ReentrantLockClass r = new ReentrantLockClass();
A a = new A(r);
B b = new B(r);
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}
}
这是输出:
它应该 运行 一个线程然后 运行 其他线程
print
方法创建一个新的 ReentrantLock
实例。因此,每个线程都锁定自己的锁,因此它们不会相互阻塞。一种方法是在 ReentrantLockClass
'(隐式)构造函数中创建锁,然后在 print
方法中锁定它:
public class ReentrantLockClass {
// One lock shared for the instance
ReentrantLock lock = new ReentrantLock();
public void print(int count){
try {
lock.lock();
for(int i = 1 ; i <= 10 ; i ++){
System.out.println(count * i);
Thread.sleep(500);
}
}
catch (Exception e){}
finally {
lock.unlock();
}
}
}