我有一个激活2个线程的应用程序,第1个启动另一个类来进行一些处理,然后启动第3个类来进行更多处理.主类中的第二个线程应该等到第三个类中的某个事件在执行其作业之前完成.怎么能实现这一目标?
我曾尝试实现一个wait/notify来共享两个线程之间的锁对象,但从技术上讲,这不会起作用,因为我找到了困难的方法.我可以在课堂之间分享锁吗?注意,第3个类的实例在第1个类中声明,并作为参数传递给第2个类.此外,我尝试在第3类中创建布尔值,告诉事件何时完成然后轮询第2个线程,直到该值为真.这有效,但不是很理想.actionListner也是解决这个问题的更好方法吗?
具体来说,有人可以告诉我这段代码有什么问题.它应该启动线程,所以应该打印"输入线程..."5次,然后等到调用notifyAll().但是,它会随机打印"输入......"和"完成......",并且仍在等待其他人.
public class ThreadTest implements Runnable {
private int num;
private static Object obj = new Object();
ThreadTest(int n) {
num=n;
}
@Override
public void run() {
synchronized (obj) {
try {
System.out.println("Entering thread "+num);
obj.wait();
System.out.println("Done Thread "+num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Runnable tc;
Thread t;
for(int i=0;i<5;i++) {
tc = new ThreadTest(i);
t = new Thread(tc);
t.start();
}
synchronized (obj) {
obj.notifyAll();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我的应用程序需要等到特定进程开始.我是这样做的
while (Process.GetProcessesByName("someProcess").Length == 0)
{
Thread.Sleep(100);
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方式(更优雅)如何实现这一点,功能类似于WaitForExit()?谢谢你的回答.
我有个问题.当我notify()在synchronized块中使用时,我有IllegalMonitorStateException.任何人都可以帮我解决这个问题吗?
我必须这样做,一个线程将发送到第二个线程char,然后这个线程必须等待,第二个线程打印此char.在第二个线程等待之后,第一个再次发送下一个char
Main.java:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Main extends JFrame {
Thread t1, t2;
Consumer con;
public Main() {
con = new Consumer();
startThreads();
}
private synchronized void startThreads() {
t1 = new Thread(new Producent("grudzien", con));
t1.start();
t2 = new Thread(con);
t2.start();
}
public class Producent implements Runnable {
String m_atom;
char[] atoms; …Run Code Online (Sandbox Code Playgroud) 我得到了java.lang.IllegalMonitorStateException.我提到了这个问题,它解决了我的问题.第一个答案是
To be able to call notify() you need to synchronize on the same object.
synchronized (someObject) {
someObject.wait();
}
/* different thread / object */
synchronized (someObject) {
someObject.notify();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我们需要在同一个对象广告上同步它的工作原理?
据我所知,据我所知
synchronized (someObject) {
someObject.wait();
}
Run Code Online (Sandbox Code Playgroud)
我们得到对象someObject的锁,然后我们调用wait().现在,另一个线程怎么能锁定同一个对象来调用notify()呢?我错过了什么?
我有一个Windows软件包列表,我使用以下命令通过powershell安装:
& mypatch.exe /passive /norestart
mypatch.exe正在从列表中传递,它不会等待先前的安装完成 - 它只是继续.它构建了一个巨大的安装窗口,正在等待安装.此外,我无法$LASTEXITCODE确定安装是成功还是失败.
反正有没有让安装在开始下一个之前等待?
我开始使用Haxe和OpenFl,并且拥有一些Javascript和Lua的经验.
它很顺利,直到我需要一个类似于wait()Lua等的功能,它会停止脚本,直到你设置的秒数结束.
我该怎么做呢?
编辑:澄清一下,我正在构建Flash.
我有一个在一个单独的线程中初始化的对象.在填充本地数据库时,初始化可能需要几秒钟.
SpecialAnalysis currentAnalysis = new SpecialAnalysis(params_here);
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现一个"取消"按钮,它将对象的isCancelled布尔值设置为true.实现这个的正确Java方法是什么?
while (currentAnalysis == null) {
}
currentAnalysis.cancel();
Run Code Online (Sandbox Code Playgroud)
该方法冻结了程序,因为它似乎进入了计算效率低下的循环.这是我可以使用的情况Object.wait()吗?
我目前的糟糕/半成功的解决方案是:
while (currentAnalysis == null) {
Thread.sleep(500);
}
currentAnalysis.cancel();
Run Code Online (Sandbox Code Playgroud)
谢谢!
我想使用browser.wait函数重复检查按钮元素是否存在一段时间然后使用相关的回调.下面我有不使用等待的代码.
detailsButton.isPresent()
.then(function(present){
if(!present) {
callback();
} else {
callback(new Error('The details button was not present.'));
}
});
Run Code Online (Sandbox Code Playgroud)
我想帮助修复这段代码,因为我不确定wait函数如何处理falure/timeout.基本上我在问下面代码的'.then'部分应该是什么,而不是我现在所拥有的那么笨重.
browser.driver.wait(function(){
return pgTransactionHistory.transactionHistoryDetails.isPresent();
}, 60000).then(function(){
pgTransactionHistory.transactionHistoryDetails.isPresent()
.then(function(present){
if(!present) {
callback();
} else {
callback(new Error('The details button was not present.'));
}
});
});
Run Code Online (Sandbox Code Playgroud)
谢谢!
我是MPI的新手,这个程序是用C语言编写的.在以下程序中,我要求其他处理器打印消息.但是我想在所有其他进程完成后在进程/等级0打印"END"消息.要运行此程序,我使用4个处理器并执行以下命令mpicc file.c -o objfile,mpirun -np 4 objfile
如果可能,请向我显示示例.
#include <mpi.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int i;
double centroid[3];/*ignore this array*/
if (world_rank == 0)
{
int destination;
for (i=0; i<3; i++)
{
/*Ignore centroid buffer been sent for now*/
destination = i+1;/*destination rank or process*/
MPI_Send(¢roid, 3, MPI_DOUBLE, destination, 0, MPI_COMM_WORLD);
}
printf("\nEND: This need to print after all MPI_Send/MPI_Recv has …Run Code Online (Sandbox Code Playgroud)