在阅读果壳中的C#3.0的约瑟夫和Ben阿尔巴哈利,我碰到下面的段落来了(673页,在标题的部分第一段" 与等待信令和脉冲 ")
" Monitor类通过两种静态方法Wait和Pulse提供另一种信令构造.原理是您使用自定义标志和字段(包含在锁定语句中)自己编写信令逻辑,然后引入Wait和Pulse命令来缓解CPU旋转这种低层次的方法的优点是,只有等待,脉搏和锁定语句,可以实现的功能的AutoResetEvent,ManualResetEvent的,和信号量,以及WaitHandle中的静态方法为WaitAll和了WaitAny.此外,等待和脉冲 可以在所有的等待句柄都吝啬挑战的情况下适用的."
我的问题是,对最后一句的正确解释是什么?
还将欣赏这种情况的有启发性的例子,以及可能通过等待和脉冲而不是通过其他方法更有效地处理它们的方式和/或原因.
谢谢!
编辑:我在这里找到了在线文本
我有以下计划:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleWaitNotify implements Runnable {
final static Object obj = new Object();
static boolean value = true;
public synchronized void flag() {
System.out.println("Before Wait");
try {
obj.wait();
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
System.out.println("After Being Notified");
}
public synchronized void unflag() {
System.out.println("Before Notify All");
obj.notifyAll();
System.out.println("After Notify All Method Call");
}
public void run() {
if (value) {
flag();
} else {
unflag();
}
}
public static void main(String[] args) throws …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我正在启动一个新的Activity,并需要在继续之前获得活动的结果.
我意识到startActivityForResult是异步/非阻塞的,我可以在onActivityResult回调中获得活动的结果.
所以我想我正在寻找的是等待活动返回的最佳方式......也许这样的事情?或者,还有更好的方法?
活动启动器功能:
public String ActivityLauncher()
{
//Set up Intent
startActivityForResult(intent, 1);
while (mIsActivityDone == false)
{
Thread.Sleep(250);
}
//Continue with processing
String data = "<Data from Activity">
return data;
}
Run Code Online (Sandbox Code Playgroud)
打回来:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Pull out the data
mIsActivityDone = true;
}
Run Code Online (Sandbox Code Playgroud)
数据需要返回到更高级别的调用函数 - 这就是我需要等待ActivityLauncher函数中的结果的原因.
谢谢!
我对这两个描述非常困惑:
这是我的问题:
我知道Java中的每个对象都有一个锁,但"监视器锁"是什么意思?是否与oject的锁相同?
为什么通知方法需要放弃显示器锁定?
如果我尝试使用以下代码使对象等待:
class simpleTask extends Thread
{
int waitingTime;
public simpleTask(int waitingTime)
{
this.waitingTime = waitingTime;
}
public void run()
{
synchronized(this) // this is a reference of current object
{
try {
this.wait(waitingTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)与上面的第一个描述一样,是指当前对象被synchronized关键字阻塞,然后wait方法释放锁?
我正在尝试创建一个C#.NET 2.0脚本来自动化用Java编写的第三方应用程序中的工作流.
我正在使用user32.dll函数来与Java应用程序窗口进行交互.
我的问题是Java应用程序窗口完全加载缓慢,我发送的操作IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, IntPtr lParam)有时会丢失.示例:在加载主窗口期间,如果我尝试打开菜单(此时我实际上在主窗口上有一个句柄),我的消息就丢失了,菜单永远不会打开.
我在Stack Overflow上找到了一些答案,告诉我们可以将该Process.WaitForInputIdle()方法用于此目的,但我发现这只适用于应用程序是单线程的,而不是我的Java应用程序的情况.(C#SendKeys在发送前等待程序加载)
我正在寻找一些工作相同的想法,但支持多线程或任何其他想法的东西?!:)
遵循Mari9的想法,我终于使用了System.Diagnostic.PerformanceCounter()观看第三方应用程序进程活动.然后它命令为0,这意味着该进程已完成它必须执行的操作并能够执行我发送它的消息.
PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "% Processor Time";
PC.InstanceName = "<process name>";
Console.WriteLine(PC.NextValue().ToString());
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
os.remove('_Temp_Dir_\main' + str(i) + '.exe')
os.rmdir('_Temp_Dir_')
Run Code Online (Sandbox Code Playgroud)
这给了我:
OSError: [WinError 145] Directory is not empty: '_Temp_Dir_'
Run Code Online (Sandbox Code Playgroud)
如果我把线
time.sleep(0.05)
Run Code Online (Sandbox Code Playgroud)
之前os.rmdir(),它正常工作.我认为os.remove()删除文件的速度还不够快.有什么方法可以等待它完成它的工作吗?
我需要执行许多命令行脚本.它们目前存储在一个List.我想同时运行它们,并且只有在所有这些步骤完成后才继续执行下一步.
我已经尝试了下面显示的方法,但发现它缺乏,因为最后一个命令不一定最后结束.事实上,我发现最后一个命令甚至可以是第一个完成的命令.所以,我相信我需要类似的东西WaitForExit(),但在所有执行过程完成之前不会返回.
for (int i = 0; i < commands.Count; i++)
{
string strCmdText = commands[i];
var process = System.Diagnostics.Process.Start("CMD.exe", strCmdText);
if (i == (commands.Count - 1))
{
process.WaitForExit();
}
}
//next course of action once all the above is done
Run Code Online (Sandbox Code Playgroud) 我有一个标准的用户名/密码/提交按钮表单,当用户点击表单提交的按钮时ng-submit="login.submit()",登录和成功使用ui.router($state.go("main"))重定向到主页面.
以下测试失败:
describe("login", function() {
beforeEach(function() {
var email = element(by.model("login.email"));
email.clear().sendKeys("mail");
var password = element(by.model("login.password"));
password.clear().sendKeys("pass");
var submit = element(by.id("submit"));
submit.click();
});
it("should be able to login", function() {
expect(element(by.css(".loginPage")).isPresent()).toBe(false);
expect(element(by.css(".mainPage")).isPresent()).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
如果我尝试添加等待时间,我可以看到浏览器一直停留在登录页面上(点击按钮后) - 然后我得到超时.
成功登录后,浏览器会收到一个带有令牌的cookie,用于验证每个后续请求.
编辑:通过一些修修补补,我发现它失败了..
function login(email, pass) {
alert("it gets here");
return _auth.post({ username: email, password: pass }).then(function(data) {
alert("does not get here");
console.log("loginok, token:" +$browser.cookies().apiToken); //this should be the received token
return data;
});
}
Run Code Online (Sandbox Code Playgroud)
EDIT2:Auth服务
var _auth = Restangular.withConfig(function(Configurer) …Run Code Online (Sandbox Code Playgroud) 考虑以下示例代码,其中线程 A 将函数推送到队列中,线程 B 在从队列中弹出时执行这些函数:
std::atomic<uint32_t> itemCount;
//Executed by thread A
void run(std::function<void()> function) {
if (queue.push(std::move(function))) {
itemCount.fetch_add(1, std::memory_order_acq_rel);
itemCount.notify_one();
}
}
//Executed by thread B
void threadMain(){
std::function<void()> function;
while(true){
if (queue.pop(function)) {
itemCount.fetch_sub(1, std::memory_order_acq_rel);
function();
}else{
itemCount.wait(0, std::memory_order_acquire);
}
}
}
Run Code Online (Sandbox Code Playgroud)
其中queue是一个并发队列,它有一个push和一个pop函数,每个函数返回一个bool指示给定操作是否成功。所以如果满了就push返回,如果空就返回。falsepopfalse
现在我想知道代码是否在所有情况下都是线程安全的。假设线程 Bpop失败并且即将调用std::atomic<T>::wait. 同时,线程 A 推送一个新元素,而线程 B 检查初始等待条件。由于itemCount尚未更改,因此失败。
紧接着,线程 A 增加计数器并尝试通知一个正在等待的线程(尽管线程 B 尚未在内部等待)。线程 B 最终等待原子,导致线程由于丢失信号而永远不会再次醒来,尽管队列中有一个元素。只有当新元素被推入队列时才会停止,通知 …
std::atomic<T>两者std::condition_variable都有成员wait和notify_one功能。在某些应用程序中,程序员可以选择使用其中之一来实现同步目的。这些wait函数的目标之一是它们应与操作系统协调以最大程度地减少虚假唤醒。也就是说,操作系统应该避免唤醒wait-ing 线程,直到notify_one或notify_all被调用。
在我的机器上,sizeof(std::atomic<T>)issizeof(T)和sizeof(std::condition_variable)is 72。如果排除std::atomic<T>的T成员,则std::condition_variable保留 72 字节用于其同步目的,同时sizeof(std::atomic<T>)保留 0 字节。
我的问题:我应该期望std::condition_variables 和std::atomic<T>swait函数之间有不同的行为吗?例如,是否应该std::condition_variable减少虚假唤醒?
wait ×10
atomic ×2
c# ×2
c++ ×2
java ×2
notify ×2
android ×1
asynchronous ×1
c++20 ×1
command-line ×1
locking ×1
login ×1
process ×1
protractor ×1
pulse ×1
python ×1
stdatomic ×1
waithandle ×1
window-load ×1
windows ×1