首页 \ 问答 \ 为什么我的线程没有完成(Why my threads are not completing)

为什么我的线程没有完成(Why my threads are not completing)

我想了解countDownLatch,我有这个程序,但我不知道为什么我的程序没有返回而没有完成。

package countDownLatches;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Processor implements Runnable {

    CountDownLatch latch;

    public Processor(CountDownLatch latch) {
        this.latch = latch;
    }

    public void run() {
        System.out.println("thread started: ");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }

}

public class App {
    public static void main(String args[]) {
        CountDownLatch latch = new CountDownLatch(3);
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            executorService.submit(new Processor(latch));
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("task completed");
    }
}

I am trying to understand countDownLatch and I have this program but I do not know why my program is not returning and not finishing.

package countDownLatches;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Processor implements Runnable {

    CountDownLatch latch;

    public Processor(CountDownLatch latch) {
        this.latch = latch;
    }

    public void run() {
        System.out.println("thread started: ");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }

}

public class App {
    public static void main(String args[]) {
        CountDownLatch latch = new CountDownLatch(3);
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            executorService.submit(new Processor(latch));
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("task completed");
    }
}

原文:https://stackoverflow.com/questions/22439547
更新时间:2023-12-18 21:12

最满意答案

从0.7版开始,Faye包含一个用于监控引擎内活动的API。 这意味着您可以附加事件侦听器来监视客户端会话的创建和销毁,找出客户端订阅和取消订阅频道的时间,以及观察已发布的消息。

您将事件侦听器附加到服务器,如下所示:

var bayeux = new Faye.NodeAdapter({mount: '/faye', timeout: 45})

bayeux.on('handshake', function(clientId) {
  // event listener logic
})

可用的事件是:

  • handshake [clientId] - 当新客户端连接并发出ID时触发。

  • subscribe [clientId,channel] - 当客户端订阅某个频道时触发。 如果收到已存在的订阅的/ meta / subscribe消息,则不会触发此消息。

  • 取消订阅[clientId,channel] - 当客户端取消订阅频道时触发。 这可能是因为客户端显式发送了/ meta / unsubscribe消息,或者因为它的会话被服务器超时了。

  • 发布[clientId,channel,data] - 发布非/ meta / **消息时触发。 包括发布者的客户端ID(可以为null),消息发送到的通道以及数据有效负载。

  • disconnect [clientId] - 在客户端会话结束时触发,或者是因为它显式发送了/ meta / disconnect消息,或者是因为它的会话被服务器超时了。

我希望这个对你有用


Since version 0.7, Faye includes an API for monitoring activity going on within the engine. This means you can attach event listeners to monitor the creation and destruction of client sessions, find out when clients subscribe and unsubscribe from channels, and watch published messages.

You attach an event listener to your server like so:

var bayeux = new Faye.NodeAdapter({mount: '/faye', timeout: 45})

bayeux.on('handshake', function(clientId) {
  // event listener logic
})

The available events are:

  • handshake [clientId] – Triggered when a new client connects and is issued with an ID.

  • subscribe [clientId, channel] – Triggered when a client subscribes to a channel. This does not fire if a /meta/subscribe message is received for a subscription that already exists.

  • unsubscribe [clientId, channel] – Triggered when a client unsubscribes from a channel. This can fire either because the client explicitly sent a /meta/unsubscribe message, or because its session was timed out by the server.

  • publish [clientId, channel, data] – Triggered when a non-/meta/** message is published. Includes the client ID of the publisher (which may be null), the channel the message was sent to and the data payload.

  • disconnect [clientId] – Triggered when a client session ends, either because it explicitly sent a /meta/disconnect message or because its session was timed out by the server.

I hope that this helps

相关问答

更多
  • 首先,是的,用after_create发布通知很好。 您应该在您的notify_subscribers方法中执行的操作是发布关于您希望在客户端中使用的新帖子的所有相关信息,以便在收到通知时不必再发出不必要的AJAX请求。 因此,例如,如果帖子的title和content与创建后立即看到的用户相关,则可以执行以下操作: def notify_subscribers client = Faye::Client.new('http://localhost:3000/faye') client.publis ...
  • 只能从外部访问Web类型进程,并且它们必须侦听$ port中指定的端口。 就像你在Procfile中的第一行一样。 如果您希望第二个进程监听端口并可从外部访问,则必须将其放入第二个应用程序。 Only web type processes are accessible from the outside and they have to listen on the port specified in $port. Just like your first line in the Procfile. If yo ...
  • 我建议你在生产环境中的Faye.Client初始化中使用你的实际ip而不是localhost (当然使用request.host helper): var faye = new Faye.Client("http://#{ request.host }:9292/faye"); I would suggest you to use your actual ip instead of localhost in Faye.Client initialization in production environm ...
  • 对于那些有同样问题的人,我的建议是无论你使用的是哪个版本的Apache,只需毫不犹豫地将其更改为nginx。 配置和运行它非常容易。 我正在使用以下配置为我的应用程序命名示例。 正确设置nginx后,其他一切都运行良好。 希望这有助于某人 # app: example # locally running on port 7000 upstream example { server 127.0.0.1:7000; server 127.0.0.1:7001; } # Faye is set ...
  • 根据我的说法,Socket.IO将致力于在每个浏览器和移动设备中实现实时应用程序,模糊不同传输机制之间的差异。 它在JavaScript中实现100%无关紧要的实时性。 欲了解更多信息,请访 http://socket.io/#how-to-use According to me Socket.IO will Works its aims to make realtime apps possible in every browser and mobile device, blurring the diffe ...
  • HAZZAH! 我想到了。 我跳过各种各样的篮球,并不是100%肯定我发现的解决方案因为我试过的其他一些东西而无法工作但是... 我尝试的第一件事是关于在Centos上安装Thin with a Rails应用程序的教程,(来自Slicehost的文档) Slicehost文章:CentOS - 用于Ruby的瘦Web服务器,并做了一大堆精简配置。 但我不相信这是必要的,因为private_pub / faye应该为你处理这一切。 (据我所知) 一个重要的事情是,我知道您需要使用private_pub描述的 ...
  • 我找到了解决问题的方法。 它发生了,因为我只验证'/ meta / subscribe'而不是客户发布消息的其他chanells。 现在,第二种推送消息的扩展方法应如下所示: def incoming(message, callback) return callback.call(message) if message['channel'].start_with? '/meta/' end I found solution for my issue ...
  • 从0.7版开始,Faye包含一个用于监控引擎内活动的API。 这意味着您可以附加事件侦听器来监视客户端会话的创建和销毁,找出客户端订阅和取消订阅频道的时间,以及观察已发布的消息。 您将事件侦听器附加到服务器,如下所示: var bayeux = new Faye.NodeAdapter({mount: '/faye', timeout: 45}) bayeux.on('handshake', function(clientId) { // event listener logic }) 可用的事件是 ...
  • 我有这个工作,虽然我不确定为什么。 这是我做的: 基于这篇文章中的一些讨论,我在代码中添加了以下内容,以便更好地了解发生了什么: html_text = render_to_string :partial => 'my_partial', :formats => [:html] test_text = "

    Testing this

    " puts "****************************************" puts html_text ...
  • 你认识Foreman吗? 使用工头,您可以设置命令以在单个文件上启动您的应用程序,如下所示 web: bundle exec start_app_command faye: bundle exec start_faye_command 然后,只需运行bundle exec foreman start和foreman将运行rails和faye Do you know Foreman? With foreman you can set the commands to start your app on a s ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。