gpt4 book ai didi

java - 使用线程锁定系统的偶数奇数

转载 作者:行者123 更新时间:2023-11-30 06:06:36 29 4
gpt4 key购买 nike

今天我在线程上做了一些练习,并尝试创建一个偶数奇数程序。我使用同步创建了这个,并且运行良好。但是当我尝试使用 Lock 做同样的事情时,我卡住了。

下面是我尝试执行相同操作的代码。

public class OddEvenNumberThreadLock {

public static void main(String args[]) {

SharedObject sharedObject = new SharedObject();

Thread evenThread = new Thread(new EvenNumber(sharedObject));
Thread oddThread = new Thread(new OddNumber(sharedObject));

evenThread.start();
oddThread.start();
}

}



class EvenNumber implements Runnable {
SharedObject object;

public EvenNumber(SharedObject object) {
this.object = object;
}

@Override
public void run() {

for (int i = 0; i <= 100; i = i + 2) {

while (!object.isOdd()) {
object.getLock().lock();
try {
System.out.println("Even : " + i);
object.setOdd(true);
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();

}

}

}

}

}
class OddNumber implements Runnable {
SharedObject object;

public OddNumber(SharedObject object) {
this.object = object;
}

@Override
public void run() {

for (int i = 1; i <= 100; i = i + 2) {

while (object.isOdd()) {
object.getLock().lock();
try {
System.out.println("Odd : " + i);
object.setOdd(false);
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();

}

}

}

}
}

class SharedObject {
private Lock lock;
private boolean isOdd;

public SharedObject() {
this.lock = new ReentrantLock();

}

public boolean isOdd() {
return isOdd;
}

public void setOdd(boolean isOdd) {
this.isOdd = isOdd;
}

public Lock getLock() {
return lock;
}

public void setLock(Lock lock) {
this.lock = lock;
}
}

我还有一个问题,就像在同步的情况下,我们使用通知方法来通知其他线程。在Lock的情况下我们如何实现这个目标。

谢谢

最佳答案

据我所知,您希望实现您的两个线程以乒乓方式打印偶数和奇数。使用 ReentrantLock 比使用 synchronized block 更容易实现您想要的行为,因为 synchronized 总是不公平的,但您可以使 ReentrantLock 公平地说,使用适当的构造函数。以下是使用 Lock 后程序的样子:

public class App {

public static void main(String args[]) {

SharedObject sharedObject = new SharedObject();

Thread evenThread = new Thread(new EvenNumber(sharedObject));
Thread oddThread = new Thread(new OddNumber(sharedObject));

evenThread.start();
oddThread.start();
}

}


class EvenNumber implements Runnable {
SharedObject object;

public EvenNumber(SharedObject object) {
this.object = object;
}

public void run() {
int i = 0;
while(i <= 100) {
object.getLock().lock();
try {
if (!object.isOdd()) {
System.out.println("Even : " + i);
i = i + 2;
object.setOdd(true);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();

}
}
}
}

class OddNumber implements Runnable {
SharedObject object;

public OddNumber(SharedObject object) {
this.object = object;
}

public void run() {
int i = 1;
while(i <= 100) {

object.getLock().lock();
try {
if(object.isOdd()) {
System.out.println("Odd : " + i);
i = i + 2;
object.setOdd(false);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();

}

}

}
}

class SharedObject {
private Lock lock;
private boolean isOdd;

public SharedObject() {
this.lock = new ReentrantLock(true);

}

public boolean isOdd() {
return isOdd;
}

public void setOdd(boolean isOdd) {
this.isOdd = isOdd;
}

public Lock getLock() {
return lock;
}

public void setLock(Lock lock) {
this.lock = lock;
}

}

关于java - 使用线程锁定系统的偶数奇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51178385/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com