- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我有一个经典案例“我的代码有效,但我不知道为什么”。
我有一个创建线程的程序,当我从扫描器接收到特定输入时,我将字符串的控制权传递给工作线程。为此,我让我的线程等待 (),当我从我的 UI 线程获得正确的输入时,我通知 ()。
这是我的代码。为简单起见,我只使用了一个线程。
package main;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
volatile Boolean keepRunning = true;
private Queue<String> q = new LinkedList<String>();
ThreadDemo( String name){
threadName = name;
System.out.println("Creating " + threadName );
}
public void in(String ex){
q.add(ex);
System.out.println("Added " + ex + "to queue of " + threadName);
synchronized(t){
t.notify();
}
}
public void run() {
System.out.println("Starting to loop.");
while (keepRunning) {
try {
//Why does it matter that I synchronized t?
synchronized(t){
System.out.println(threadName + "Waiting");
t.wait();
}
} catch (InterruptedException e) {
System.out.println("Thread interrupted " + e.toString());
}
System.out.println(threadName + "notified");
if (q.size()>0){
String out = q.remove();
System.out.println(threadName + "received " + out);
}
}
System.out.println("Done looping.");
}
public void start ()
{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
}
public class DataAnalysisProgram {
public static void main(String[] args) {
ThreadDemo T1 = new ThreadDemo( "Thread-1");
T1.start();
System.out.println("say something");
Scanner s = new Scanner(System.in);
String t;
do{
t = s.next();
T1.in(t);
}while (!t.equals("stop"));
T1.keepRunning = false;
T1.interrupt();
s.close();
}
}
所以这很好用。我的线程一直等到我使用通知。但是,我真的不明白我在哪个对象上调用 notify
和 wait
的意义。
在我的实现中,我随意地执行了 t.wait()/t.notify()
,其中 t 是我的线程对象。我想如果我执行 threadName.wait()/threadName.notify()
它仍然有效。为什么我们要在看似任意的对象上调用 notify 和 wait?我知道我在这里遗漏了一个关于通知和等待的概念。
最佳答案
事实上,当您在 Thread
实例上调用 wait
时,您违反了契约:
It is recommended that applications not use
wait
,notify
, ornotifyAll
onThread
instances.
这是因为 Thread
将其用于其内部目的。
回答您的问题:Thread
对象不是线程。说 t.notify()
不通知 t
,它通知等待 t
监视器的线程。在此上下文中,Thread
的实例只是另一个 Java 对象,所有 Java 对象都有一个与之关联的监视器。
您建议使用 String threadName
的监视器是另一个坏主意,因为您无法控制字符串实例的生命周期,并且可能很容易践踏内部字符串的问题。
建议的做法是不要在线程协调中涉及任意对象的监视器,但更愿意为此使用 Object
的专用实例。这是由关注点分离原则的一般优势插入的。
关于java - 如果我只想要一种在线程之间发出信号的方法,为什么我在哪个对象上使用 wait()/notify() 很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26157606/
我正在尝试在我安装的 Laravel 5.1 中设置 Gulp。我已经运行然后命令 npm install正如 Laravel 文档中所指定的那样,效果很好。 但是,当我现在运行命令时 gulp我收到
我有两个扩展线程的类和一个等待/通知 class A extends Thread { int r = 20; public void run() { try {
我正在尝试在后台服务中启动通知,这也是位置监听器。我有一个功能: public Notification CreateNotification(){ Intent notificati
编辑:我将其范围缩小到 python 代码中 Notifier.notify('Something') 的问题。当 python 脚本从 launchd 启动时,这不会产生预期的行为。我的其他 pyt
我正在尝试使用 bootstrap-notify v3.1.3、typescript、aurelia 和 VS2015 在我的网站上显示警报。 我的 aurelia 组件是: //myAlert.ts
注:other question的标题不同,导致无法识别为匹配的。 系统.类 TCollection = class(TPersistent) protected procedure Notify
我正在将一个项目从 Sprockets 迁移到 Webpacker。 我似乎无法正确运行的最后一件事是通知。 我曾经能够做到:$.notify('Test') 但现在我得到了 Uncaught Typ
我在一个项目中有多个用户控件,其中一个从 XML 中检索项目,创建“ClassItem”类型的对象,并应通知其他用户控件有关这些项目的信息。 我为我的对象创建了一个类(所有项目都将具有的“模型”):
我以为我会理解 Java 中的并发概念,但现在有一件事打破了我的理解: 为什么我必须在同步块(synchronized block)中包含对 wait() 和 notify() 方法的调用? 假设我有
我正在开发 NodeJS/Electron/Angular 应用程序,我正在使用 node-notifier 模块。一切正常,但在我的通知消息底部有一个不合适的“toast”字符串。它仅在我使用“图标
我正在基于Sproutcore 1.9.1的Web应用程序上工作。要从服务器检索数据 发出一个SC.Request.getUrl()请求,该请求在除IE8之外的所有浏览器中都可以正常工作。 对于IE8
我试图写一个关于如何使用wait()和notify()的示例,但是似乎无法通知wait() public class Transfer { private int[] data; pr
我想使用QAudioInput从麦克风捕获声音,对其进行处理然后再播放。据我了解,我需要连接到notify信号和内部处理程序,以便用户使用readAll()函数来获取原始数据。但是问题是,此处理函数永
public class Signal2NoiseRatio { public ImagePlus SingleSNR(ImagePlus imagePlus) throws Interrup
为什么只有第一个任务退出后才调用dispatchGroup.notify? 下面的代码输出如下: 1) Did the other thing **2) Did all the things** 3)
假设线程 T1 正在等待进入同步块(synchronized block),线程 T2 在同步块(synchronized block)内 wait(),并且线程 T3 在同步块(synchroniz
我正在尝试仅使用等待/通知同步来实现合并排序。我知道更高级的结构,例如 Fork/Join、Executors。等等,但我需要在这里使用工作/通知。基于此https://courses.cs.wash
了解 notifyAll 让我对 notify 产生了一些疑问:在典型情况下,我们有多个线程正在等待对 methody notify 的调用。当这种情况发生时,其中一个线程(之前调用了 wait 方法
我正在使用 wait() 和 notify() 编写一个示例程序,但是当调用 notify() 时,会出现多个线程被唤醒而不是一个。 代码是: public class MyQueue { O
根据17.2.4. Interactions of Waits, Notification, and Interruption : Similarly, notifications cannot be
我是一名优秀的程序员,十分优秀!