- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经找到good hints here 。但我有更困难的任务 - 额外的要求是:
-我的低优先级永久线程可以从主线程启动/停止(这是相同的)-但它还必须锁定一个资源以进行独占访问。
-我的低优先级永久线程可以从另一个高优先级线程暂停/继续(然后它们也锁定并使用该资源)
-我还希望低优先级线程不要在每个循环中释放锁,而只在被告知时释放锁(出于速度目的 - 因为我需要初始化/取消初始化资源)(如果我有在每个循环中释放锁,然后我可以让java来管理竞争线程并期望高优先级线程时不时地获胜)。
我想出了解决方案,我认为它是安全可靠的。但我是新手,所以我愿意在我的解决方案中查找错误或改进建议。所以我的问题是:
1)我的解决方案在所有情况下都真的是线程安全的吗?
2)这是最佳解决方案还是我可以改进一些东西?
如果您同意,则将其用作模板。
核心代码如下:
FlagNotify lowPriorRunReq = new FlagNotify(false); // low priority task run request
FlagNotify lowPriorPauseReq = new FlagNotify(false); // low priority task pause request (it uses notify)
volatile boolean lowPriorRunFlag = false; // low priority task run flag
Lock groupLock = new ReentrantLock(); // group lock (used to acquire lowPriorRunFlag always correctly)
Semaphore resourceSemaphore = new Semaphore(1); // main semaphore protecting resource that has to be accessed sequentially
public class PrioritySingleTaskThread extends Thread {
@Override
public void run() {
prn("High Priority Task created");
groupLock.lock();
if(lowPriorRunFlag == true) lowPriorPauseReq.setNotify(true);
resourceSemaphore.acquireUninterruptibly();
groupLock.unlock();
accessResource("high priority task");
resourceSemaphore.release();
groupLock.lock();
if(lowPriorPauseReq.get() == true) lowPriorPauseReq.setNotify(false);
groupLock.unlock();
prn("High Priority Task closed");
}
}
public class LowPriorityContinuousThread extends Thread {
void getResourceSemaphore(){
groupLock.lock();
resourceSemaphore.acquireUninterruptibly();
lowPriorRunFlag = true;
groupLock.unlock();
accessResource("low priority init"); // here it is initialization and I want to happen only on request from priority thread
}
void releaseResourceSemaphore(){
accessResource("low priority de-init"); // here it is de-initialization and I want to happen only on request from priority thread
lowPriorRunFlag = false;
resourceSemaphore.release();
}
@Override
public void run() {
while(true){
//prn("Low Priority Run req: "+lowPriorRunReq.get());
if(lowPriorRunReq.get() == true && lowPriorRunFlag == false){
prn("Low Priority Task starting");
getResourceSemaphore();
prn("Low Priority Task started");
}
if(lowPriorRunReq.get() == false && lowPriorRunFlag == true){
prn("Low Priority Task stopping");
releaseResourceSemaphore();
prn("Low Priority Task stopped");
lowPriorRunReq.smartWait(true);
}
// note keep checking lowPriorRunFlag. Imagine there is RunFlag detected by high priority thread
// before de-asserted, then pauseReq would be requested from high priority thread
// then resource is released when low priority task stops.
// High priority lock and use resource, but since pauseReq is set
// this thread would try to access device in order to de-init and pause (unless we check runFlag)
if(lowPriorPauseReq.get() == true && lowPriorRunFlag == true){
prn("Low Priority Task pausing");
releaseResourceSemaphore();
prn("Low Priority Task paused");
lowPriorPauseReq.smartWait(false);
getResourceSemaphore();
prn("Low Priority Task continue");
}
if(lowPriorRunFlag){
accessResource("low priority task");
}
}
}
}
这里是完整的可编译 Java 代码,包括测试平台(所以我暗示这是一个安全的解决方案 - 但你永远不知道这些线程)
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
// this inner class is only for setting flag and waiting to it by notify - which does not hog CPU
public class FlagNotify{
private Boolean flag; // do not synchro on Boolean - it is immutable, thus new object is created every value change....
private Object synchro = new Object();
public FlagNotify(boolean val) {
flag = val;
}
public void setNotify(boolean val) {
synchronized (synchro) {
flag = val;
synchro.notify();
}
}
public boolean get(){
return flag;
}
public void smartWait(boolean expVal){
synchronized (synchro){
while(flag != expVal){
try {
synchro.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
FlagNotify lowPriorRunReq = new FlagNotify(false); // low priority task run request
FlagNotify lowPriorPauseReq = new FlagNotify(false); // low priority task pause request (it uses notify)
volatile boolean lowPriorRunFlag = false; // low priority task run flag
Lock groupLock = new ReentrantLock(); // group lock (used to acquire lowPriorRunFlag always correctly)
Semaphore resourceSemaphore = new Semaphore(1); // main semaphore protecting resource that has to be accessed sequentially
public class PrioritySingleTaskThread extends Thread {
@Override
public void run() {
prn("High Priority Task created");
groupLock.lock();
if(lowPriorRunFlag == true) lowPriorPauseReq.setNotify(true);
resourceSemaphore.acquireUninterruptibly();
groupLock.unlock();
accessResource("high priority task");
resourceSemaphore.release();
groupLock.lock();
if(lowPriorPauseReq.get() == true) lowPriorPauseReq.setNotify(false);
groupLock.unlock();
prn("High Priority Task closed");
}
}
public class LowPriorityContinuousThread extends Thread {
void getResourceSemaphore(){
groupLock.lock();
resourceSemaphore.acquireUninterruptibly();
lowPriorRunFlag = true;
groupLock.unlock();
accessResource("low priority init"); // here it is initialization and I want to happen only on request from priority thread
}
void releaseResourceSemaphore(){
accessResource("low priority de-init"); // here it is de-initialization and I want to happen only on request from priority thread
lowPriorRunFlag = false;
resourceSemaphore.release();
}
@Override
public void run() {
while(true){
//prn("Low Priority Run req: "+lowPriorRunReq.get());
if(lowPriorRunReq.get() == true && lowPriorRunFlag == false){
prn("Low Priority Task starting");
getResourceSemaphore();
prn("Low Priority Task started");
}
if(lowPriorRunReq.get() == false && lowPriorRunFlag == true){
prn("Low Priority Task stopping");
releaseResourceSemaphore();
prn("Low Priority Task stopped");
lowPriorRunReq.smartWait(true);
}
// note keep checking lowPriorRunFlag. Imagine there is RunFlag detected by high priority thread
// before de-asserted, then pauseReq would be requested from high priority thread
// then resource is released when low priority task stops.
// High priority lock and use resource, but since pauseReq is set
// this thread would try to access device in order to de-init and pause (unless we check runFlag)
if(lowPriorPauseReq.get() == true && lowPriorRunFlag == true){
prn("Low Priority Task pausing");
releaseResourceSemaphore();
prn("Low Priority Task paused");
lowPriorPauseReq.smartWait(false);
getResourceSemaphore();
prn("Low Priority Task continue");
}
if(lowPriorRunFlag){
accessResource("low priority task");
}
}
}
}
//-------------------------------------------------------------------------
//-- following functions are meant only for testing
AtomicInteger clashDetector = new AtomicInteger(0); // only for testing purposes
public void accessResource(String from){
prn("Resource used from "+from);
if(clashDetector.addAndGet(1)>1){
System.out.println("Clash detected - you are a bad programmer :((((((");
System.exit(-1);
}
sleepRandom(5);
clashDetector.getAndAdd(-1);
}
public void sleepRandom(long maxMiliSec){
mySleep((long)(Math.random()*maxMiliSec));
}
public void mySleep(long miliSec){
try{
Thread.sleep(miliSec);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
void prn(String s){
System.out.println(s);
}
public void test(){
new LowPriorityContinuousThread().start();
for(long i=0; i< (long)1e3; i++){
lowPriorRunReq.setNotify(true);
for(int j=0; j<Math.random()*100;j++){
sleepRandom(10);
new PrioritySingleTaskThread().start();
}
//sleepRandom(20);
lowPriorRunReq.setNotify(false);
for(int j=0; j<Math.random()*20;j++){
sleepRandom(10);
new PrioritySingleTaskThread().start();
}
//sleepRandom(20);
}
mySleep(200);
System.out.println("Test OK :)))))))))))))))))))))");
mySleep(200);
System.exit(0);
}
public static void main(String[] args) throws Exception {
new Main().test();
}
}
最佳答案
我不知道“启动/停止”“永久线程”意味着什么,但可用于暂停线程的一种设计模式称为“十字转门”。当只有一个 Controller 线程被允许“锁定”或“解锁”旋转门时,以下是有效的:
import java.util.concurrent.Semaphore;
class Turnstile {
private final Semaphore semaphore = Semaphore.new(1);
// Called only from the "controller" thread.
public void lock() {
semaphore.acquire();
}
// Called only from the "controller" thread.
public void unlock() {
semaphore.release();
}
// Called from worker threads.
public void passThrough() {
semaphore.lock();
semaphore.release();
}
}
最初,旋转门处于“解锁”状态,当工作人员调用 passThrough() 方法时,它会立即返回。如果主线程“锁定”旋转栅门,则任何调用 passThrough() 的工作线程都将被阻塞,直到主线程再次“解锁”它。然后,所有的worker都会一一“通过”。
<小时/>如果您想要拥有多个“主人”,您可以修改此示例,但是当一个主人希望锁定十字转门而另一个主人希望将其解锁时,由您决定如何解决冲突。
<小时/>再说一次,而不是修改Turnstile
,你可以写一个新的 MultiMasterTurnstile
处理冲突解决并使用 Turnstile
真正阻止 worker 。
关于java - 从另一个线程暂停线程,并从另一个线程停止/启动它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30923288/
我在 Window 2008 x64 上的 IIS 7 下托管了一个网站。 IIS 以 64 位模式运行,该站点有自己的 64 位应用程序池等。该网站大部分时间似乎运行良好,然后每小时突然卡住用户请求
我有一个 imageView 并希望它像这样工作: ImageViewer可见 5秒暂停 ImageView 不可见 5秒暂停 ImageViewer可见 等等... 我该怎么做?我试过 sleep
我在我的 JavaScript 函数中使用了下面的代码。我想在 10 秒后调用这个函数。然而该函数立即被触发!?!不确定发生了什么。 function testing() { //oth
我想暂停计时器,点击按钮后我想继续计时器计数...我搜索但找不到与此相关的功能.. 怎么办? 最佳答案 您将需要一个变量来跟踪自 Chronometer 启动以来耗时: long timeWhenSt
我目前有一个程序可以从麦克风收集声音信号并在 python 上实时显示波形。对于 matplotlib funcanimation,我正在尝试通过这种方式向我的程序添加一些暂停和启动按钮或功能。但它没
我有一个由套接字提供的热Observable。我可以使用pausable暂停套接字供稿。但是一旦“取消暂停”可观察对象,就需要显示套接字在暂停订阅时可能发送的最后一个值。我不想跟踪套接字手动发送的最后
我知道这是可能的,但我还没有找到方法,所以我在问。 在播放 3rd 方音乐(例如 Spotify)时开始在我的应用程序中播放 mp3 声音时。 Spotify 暂停,您必须恢复 Spotify,让它再
我正在尝试使用 iPhone 的前置摄像头录制有声视频。因为我还需要支持暂停/恢复功能,所以我需要使用 AVAssetWriter .我在网上找到了一个用 Objective-C 编写的示例,它几乎实
我知道互斥锁可以作为一种实现,但是我想知道是否有一种方法可以像视频播放一样暂停/恢复另一个线程。当其他正在运行的线程很复杂时,此方法更易于编程。 最佳答案 SIGTSTP是用于暂停进程的信号,如果您有
到目前为止,我已经看到了以下停止动画的技术,但我在这里寻找的是旋转 View 停止在当前的角度,而不是返回到 0。 struct DemoView: View { @State private
我一般在问有关多线程的问题。例如我锁定了一个互斥锁并恢复任务,然后我想挂起它,我的问题是,我应该在挂起之前解锁互斥锁吗?这样当我再次使用互斥锁恢复它时,它会成功恢复吗? 我刚刚开始使用多线程的东西,我
我有2个缩略图链接,单击它们时,它们会以灯箱样式打开视频。我的目标是让它们在打开时播放,在关闭时暂停(单击背景区域时关闭)。 我的HTML代码在这里: M
到目前为止,我没有将我发现的几种不同方法拼凑在一起: http://192.185.121.49/~steveobr/ 我需要所有的语音演示像第一个“商业”一样工作 正如您在实时示例中看到的那样,每个
所以我正在制作某种游戏,玩家可以在其中获得一些能力。玩家回合结束后,服务器应有 5 秒的超时时间,其中不执行任何代码,然后在该时间后结束回合。但是,如果客户端单击其中一项电源,服务器应停止 5 秒超时
我尝试将自己的方法添加到 Tween 类中以暂停/恢复所有补间。这就是我所拥有的: createjs.Tween.pauseAllTweens = function() { for ( var
我对 Azure 搜索标准级别的成本有疑问。是否可以将 Azure 搜索级别从标准更改为基本?是否可以暂时暂停 Azure 搜索标准?我在门户中没有看到此控件。我是否需要将 Azure 搜索实现重新创
如何用相同的代码制作play/Pause按钮。 - (IBAction)min:(id)sender { NSString *path = [[NSBundle mainBundle] pathF
我知道这很可能超出了沙箱范围,但我还是想问一下: 我想在我的应用程序中放置一个“暂停/播放”按钮,以暂停或播放任何背景音频。基本上,我希望实现在多任务栏中找到的播放/暂停按钮。 一个简单的例子是有人用
我正在制作一款编程游戏,玩家可以在其中对盟友的行为进行编程。玩家为给定的盟友编写decide()函数的主体,可以用任何java代码填写,但必须返回一个 Action 。我想为每个盟友提供一组有限的每个
我有功能 1 用于播放音乐,第二个用于设置实际音乐的暂停,我的暂停功能不起作用。我该如何设置暂停? function play(id){ var audio = new Audio('
我是一名优秀的程序员,十分优秀!