Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 等待最后一个线程通知它们的线程_Java_Multithreading_Wait_Notify - Fatal编程技术网

Java 等待最后一个线程通知它们的线程

Java 等待最后一个线程通知它们的线程,java,multithreading,wait,notify,Java,Multithreading,Wait,Notify,我希望线程等待最后一个线程使用br并通知其他线程。但是它在进入第一个等待()时被卡住了,我错过了什么 public class CrawlerThread implements Runnable { private BufferedReader br; private FileHandler fileHandler; private File sourceFile; private String skillString; private Map<String, String> url

我希望线程等待最后一个线程使用br并通知其他线程。但是它在进入第一个
等待()
时被卡住了,我错过了什么

public class CrawlerThread implements Runnable {
private BufferedReader br;
private FileHandler fileHandler;
private File sourceFile;
private String skillString;
private Map<String, String> urlData = new HashMap<String, String>();
private String urlFirst = Initializer.urlFirst;

public static Integer threadCount = 0;

public CrawlerThread(BufferedReader br, FileHandler fileHandler,
        File sourceFile, String skillString, Map<String, String> urlData) {
    this.br = br;
    this.fileHandler = fileHandler;
    this.sourceFile = sourceFile;

    this.skillString = skillString;
    this.urlData.putAll(urlData);
    new Thread(this).start();
}

@Override
public void run() {
    System.out.println("!!!!");

    String companyName;
    String searchString;
    SearchObject searchObject = new SearchObject();
    try {String c;
        while ((c=br.readLine())!=null && c.equalsIgnoreCase("Company Name")) {
            try {
                if ((companyName = br.readLine().trim()) != null) {
                    if (threadCount == (Initializer.MAX_THREAD - 1)) {
      synchronized(br){
                        System.out.println("++");
                        br.close();
                        br.notifyAll();}

                    } else
                        try {
                            System.out.println("**" + threadCount);
                            synchronized (br) {
                                synchronized (threadCount) {
                                    threadCount++;
                                }
                                br.wait();
                            }
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
公共类CrawlerThread实现可运行{
专用缓冲读取程序br;
私有文件处理程序文件处理程序;
私有文件源文件;
私有字符串skillString;
私有映射urlData=newhashmap();
私有字符串urlFirst=Initializer.urlFirst;
公共静态整数threadCount=0;
公共爬虫读取(BufferedReader br、FileHandler、FileHandler、,
文件源文件、字符串skillString、映射URL数据){
这个。br=br;
this.fileHandler=fileHandler;
this.sourceFile=sourceFile;
this.skillString=skillString;
this.urlData.putAll(urlData);
新线程(this.start();
}
@凌驾
公开募捐{
System.out.println(“!!!!”);
字符串公司名称;
字符串搜索字符串;
SearchObject SearchObject=新的SearchObject();
试试{String c;
while((c=br.readLine())!=null&&c.equalsIgnoreCase(“公司名称”)){
试一试{
if((companyName=br.readLine().trim())!=null){
if(threadCount==(Initializer.MAX_THREAD-1)){
已同步(br){
System.out.println(“+”);
br.close();
br.notifyAll();}
}否则
试一试{
System.out.println(“**”+线程计数);
已同步(br){
已同步(线程数){
threadCount++;
}
br.等待();
}
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}

要使用wait/notify,两个线程都应该获取共享锁,并检查条件,必要时进行修改,如果您确定只有一个线程等待
notify()
正常,如果没有,则使用
notifyAll()
,等待的线程基本上应该如下所示:

等待线程条件示例:

synchronized(lock){
  while(!condition){
    lock.wait();
  }
}
示例通知程序线程:

synchronized(lock){
  condition=true;
  lock.notifyAll();
}
public void notifyWaitingTreads(){
  latch.countDown();
}
您还可以使用
倒计时闩锁

final CountDownLatch latch=new CountDownLatch(1);
等待线程:

public void waitForCondition(){
  latch.await();
}
通知程序线程:

synchronized(lock){
  condition=true;
  lock.notifyAll();
}
public void notifyWaitingTreads(){
  latch.countDown();
}

目前还没有足够的信息。什么是
br
threadCount
,这是如何调用和执行的,等等。但是您可能想看看-这正是为此目的创建的。我猜您的线程没有在同一个br实例上同步,这会导致notifyAll()未能按预期的方式操作您的第一个问题是,在对readLine()进行任何其他操作之前,您没有对其结果进行null测试。除非您解决了这一问题,否则您的代码将不会被认真对待。老兄……我再也没有犯蝙蝠侠的错误了……br.readLine()。仅在第一个线程中等于公司名称回答这个问题