gpt4 book ai didi

java - 与线程通信的模式

转载 作者:行者123 更新时间:2023-12-01 11:52:03 25 4
gpt4 key购买 nike

我有一个包含 List 的类 OuterClass,并且有一个在 OuterClass 中启动的线程 ListWorker > 即将一些元素添加到列表中。基于对 OuterClass 的函数调用,它应该能够通知线程删除元素。最佳做法是什么?目的不是要具有阻塞数据结构(无同步),因此要在 List 上使用单个线程。

Class OuterClass {
List<String> list = new ArrayList<String>();
ListWorker worker = new ListWorker(list);

deleteLastElement() {
worker.setDeleteLastElement(true);
}
}

worker

ListWorker implements Runnable {

private List<String> list;
private volatile boolean deleteLastElement;

public void setDeleteLastElement(boolean deleteLastElement) {
this.deleteLastElement = deleteLastElement;
}

public ListWorker(List<String> list) {
this.list = list;
}

public void run() {
while(true) {
//add random elements

if(deleteLastElement) {
//delete last element
//set the boolean now to false
}
}
}

最佳答案

这未经测试,可能需要一些额外的异常处理,但大致就是这样:

ListWorker implements Runnable {

private interface Command{
void execute();
}

private List<String> list;
private BlockingQueue<ListWorker.Command> work; // give it a Blocking Queue impl.
private volatile boolean bAddRandomElements;


public synchronized void deleteLastElement() {
work.add( new Command(){
@Override
public void execute(){ /* delete Last Element of the list */ }
} );
}

public synchronized void startAddingRandom() {
work.add( new Command(){
@Override
public void execute(){ /* set switch bAddRandomElements */ }
} );
}

public synchronized void stopAddingRandom() {
work.add( new Command(){
@Override
public void execute(){ /* reset switch bAddRandomElements */ }
} );
}

public synchronized void terminate() {
work.add( new Command(){
@Override
public void execute(){ /* interrupt thread */ }
} );
}


public ListWorker(List<String> list) {
this.list = list;
}

public void run() {
while(!Thread.interrupted()) {
Command c = null;
if( bAddRandomElements ){
/* add random, assuming you add one random entry per iteration ... */
c = work.poll( /*maybe specify timeout*/ ); // No command - just go on with it! We'll block in next iteration if bAddRandomElements is reset.
}else{
c = work.take(); // blocks until there is a command in queue.
}

if ( null != c ) c.execute();

}
}

关于java - 与线程通信的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718811/

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