标签: countdownlatch

CountDownLatch是否受到虚假唤醒的影响?

并发管理机制(如wait/notify和lock/condition)似乎受到虚假唤醒的影响.开发人员通过重新检查条件确实已经改变来满足那些意外的唤醒.

在CountDownLatch中,虚假的唤醒是一个问题吗?

java multithreading countdownlatch

8
推荐指数
1
解决办法
472
查看次数

为什么CountDownLatch.getCount()返回一个long而不是一个int?

我查看了代码,一切都是int - 传递给CountDownLatch构造函数的参数是int,Sync中的变量是int,Sync.getCount()的返回类型是int.但CountDownLatch.getCount()返回一个长?想知道为什么.

java countdownlatch java.util.concurrent

7
推荐指数
1
解决办法
957
查看次数

Java:是CountDownLatch线程安全

文档CountDownLatch我看到类似的东西:

public void run() {
      try {
        startSignal.await();
        doWork();
        doneSignal.countDown();
      } catch (InterruptedException ex) {} // return;
}

这里startSignaldoneSignalCountDownLatch对象.

文档没有提到关于类是否是线程安全的.

java thread-safety countdownlatch

7
推荐指数
1
解决办法
2018
查看次数

Angular 2多个倒计时管道

我正在寻找创造角2/4倒计时管道.

当然我可以进行个别倒计时,但我该如何创建多个倒计时?

我想要以下输入:

<span [time]="unix timestamp here">Countdown will count here</span>
Run Code Online (Sandbox Code Playgroud)

例如:

<span [time]="1500503492">Countdown will count here</span>
<span [time]="15005034392">Countdown will count here</span>
<span [time]="1500503492">Countdown will count here</span>
Run Code Online (Sandbox Code Playgroud)

无论我有多少人,我怎样才能实现这一切?

到目前为止我尝试过的只是单次倒计时,如下所示:

time = 30;
setInterval(() => {
  this.currentTime = new Date().getTime();
  if (this.time > 0) {
    this.time = this.time - 1;
  }
}, 1000);

{{ time}}
Run Code Online (Sandbox Code Playgroud)

javascript countdownlatch countdown angular2-directives angular

7
推荐指数
1
解决办法
2652
查看次数

使用超时多次调用CountDownLatch.await(int)

我使用CountDownLatch来等待来自另一个组件的某个事件(在不同的线程中运行).以下方法适合我的软件的语义,但我不确定它是否像我期望的那样工作:

mCountDownLatch.await(3000, TimeUnit.MILLISECONDS)
otherComponent.aStaticVolatileVariable = true;
mCountDownLatch.await(3500, TimeUnit.MILLISECONDS);
... <proceed with other stuff>
Run Code Online (Sandbox Code Playgroud)

方案应该如下:我等待3秒钟,如果锁存器没有倒计数到0,我用该变量通知另一个组件,然后我等待最多3.5秒.如果再次出现超时,那么我不在乎并将继续进行其他操作.

注意:我知道它看起来不像那样,但上面的场景在我的软件中是完全合理且有效的.

我确实阅读了await(int,TimeUnit)和CountDownLatch的文档,但我不是Java/Android专家,所以我需要确认.对我来说,所有场景看起来都有效:

  • 如果第一次等待成功,则另一次等待将立即返回
  • 如果第一个等待超时,则另一个等待仍然有效; 因此,如果另一个线程注意到静态信号,则第二个await可能会成功返回
  • 两个都等待调用超时(根据我的软件的语义,这很好)

我正确使用等待(...)吗?即使先前await(...)在同一个对象上超时,也能以上述方式使用第二个等待(...)吗?

java android synchronization countdownlatch

6
推荐指数
1
解决办法
2904
查看次数

当计数为 0 时,线程在 CountDownLatch wait() 上阻塞

在分析其中一个生产环境的日志时,我在倒计时锁存器等待()上看到一个处于“等待”状态的线程

...sun.misc.Unsafe.park(Native Method)
...java.util.concurrent.locks.LockSupport.park(Unknown Source)
...java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)
...java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(Unknown Source)
...java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(Unknown Source)
...java.util.concurrent.CountDownLatch.await(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

锁存器被初始化为 1,另一个线程确实在锁存器的同一实例上调用了 countDown() 方法,但主线程仍然在锁存器上被阻塞。这导致了jvm无限期挂起。

即使锁存器计数达到零也被阻止听起来不合理,我正在寻找进一步解决此问题的建议。

有任何想法吗?

注意-使用的jvm版本如下

java 版本“1.5.0_15”Java(TM) 2 运行时环境,标准版(内部版本 1.5.0_15-b04) Java HotSpot(TM) 客户端 VM(内部版本 1.5.0_15-b04,混合模式,共享)

更新 - 下面是我上面讨论的线程的代码片段

private class MyRunnable implements Runnable, Thread.UncaughtExceptionHandler {

        private AtomicBoolean shouldStop = new AtomicBoolean(false);
        private CountDownLatch stopLatch = new CountDownLatch(1);
        private Thread currentThread;

    public void run() {

        Thread.currentThread().setName("My Thread");
            Thread.currentThread().setUncaughtExceptionHandler(this);
            currentThread = Thread.currentThread();
            if (currentThread.isInterrupted()) {
                logger.debug("The pool thread had its interrupted stattus set. Clearing...");
                Thread.interrupted();
                logger.debug("The pool …
Run Code Online (Sandbox Code Playgroud)

java countdownlatch

5
推荐指数
1
解决办法
6167
查看次数

如何使用CompletableFuture返回值

我创建了一个示例,我想知道如何使用CompletableFuture?返回值?我也改变了CompletableFuture<Void> exeFutureList,CompletableFuture<Integer> exeFutureList但是eclipse总是建议把它设置回Void.

请告诉我如何使用CompletableFuture返回值.

代码:

    public class MainClass {

    static ExecutorService exe = null;
    static CompletableFuture<Void> exeFutureList = null;

    public static void main(String[] args) {
        exe = Executors.newFixedThreadPool(1);
        exeFutureList = CompletableFuture.runAsync(new RunClass(8), exe);
    }

    static class RunClass implements Runnable {

        private int num;

        public RunClass(int num) {
            // TODO Auto-generated constructor stub
            this.num = num;
        }

        public void run() {
            // TODO Auto-generated method stub
            this.num = this.num + 10;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

java multithreading callable countdownlatch completable-future

5
推荐指数
1
解决办法
6053
查看次数

倒计时锁存和进一步同步

假设我有以下类定义,当一个线程想要为多个(可能)等待线程设置 a 时:

\n\n
public class A {\n\xc2\xa0\xc2\xa0\xc2\xa0 private int a;\n\xc2\xa0\xc2\xa0\xc2\xa0 private CountDownLatch gate;\n\n\xc2\xa0\xc2\xa0\xc2\xa0 public A(int a) {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 a = 1;\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 gate = new CountDownLatch(1);\n\xc2\xa0\xc2\xa0\xc2\xa0 }\n\n\xc2\xa0\xc2\xa0\xc2\xa0 public int getA() {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 latch.await();\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 return a;\n\xc2\xa0\xc2\xa0\xc2\xa0 }\n\n\xc2\xa0\xc2\xa0\xc2\xa0 public void setA(int a) {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 this.a = a;\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 gate.countDown();\n\xc2\xa0\xc2\xa0\xc2\xa0 }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我看来, a 需要是易失性的,但我不确定\xe2\x80\xa6 有人可以分享一下为什么(如果有的话)需要围绕 getA 进行额外的同步,或者 a 需要是易失性的吗?

\n

java multithreading synchronization countdownlatch

5
推荐指数
2
解决办法
1380
查看次数

在继续异步之前等待retrofit2完成的最佳方法

我意识到类似的问题已被问到,但我是android的新手,并且发现答案有点令人困惑,因为它们的情况略有不同.

我查看了CountDownLatch以及使用Threads,我不确定使用哪种方法.任何帮助将非常感激.我也尝试使用apply()而不是commit()来实现SharedPreferences.

我正在从LoginActivity进行2次retrofit2调用.我需要第一次调用中的令牌才能在第二次调用中使用.我将令牌保存到第一次改装调用的onResponse方法的sharedpreferences中的字符串.

在我的第二次调用中,serverToken的值将作为在应用程序的上一次运行中设置的令牌返回

第一次调用(getToken)onResponse

   call.enqueue(new retrofit2.Callback<TokenResponse>() {    

            @Override
            public void onResponse(Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {

                if (response.isSuccessful()) {
                    TokenResponse tokenResponse = response.body();

                    LoginActivity.editor.putString("serverToken", tokenResponse.getAccessToken());
                    LoginActivity.editor.commit();

                } else {
                    Log.i("Server Token", "failed");
                }
            }
 }
Run Code Online (Sandbox Code Playgroud)

LoginActivity

public class LoginActivity extends AppCompatActivity {

    public static SharedPreferences preferences;
    public static SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        authenticationController = new AuthenticationController();
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        editor = preferences.edit();
    }

    public void onLoginClicked(View view) {
        getToken();    //FIRST RETROFIT CALL
        connectToPush(); //SECOND CALL …
Run Code Online (Sandbox Code Playgroud)

android asynchronous countdownlatch runnable retrofit2

5
推荐指数
1
解决办法
6066
查看次数

CountDownLatch 中 await() 的目的是什么?

我有以下程序,我正在使用java.util.concurrent.CountDownLatch和不使用await()方法它工作正常。

我是并发的新手,想知道await(). 在CyclicBarrier我可以理解为什么await()需要,但为什么在CountDownLatch

班级CountDownLatchSimple

public static void main(String args[]) {
  CountDownLatch latch = new CountDownLatch(3);
  Thread one = new Thread(new Runner(latch),"one");
  Thread two = new Thread(new Runner(latch), "two");
  Thread three = new Thread(new Runner(latch), "three");

  // Starting all the threads
  one.start(); two.start(); three.start();
  
}
Run Code Online (Sandbox Code Playgroud)

Runner实现Runnable

CountDownLatch latch;

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

@Override
public void run() {
    System.out.println(Thread.currentThread().getName()+" is …
Run Code Online (Sandbox Code Playgroud)

concurrency multithreading countdownlatch java.util.concurrent cyclicbarrier

5
推荐指数
1
解决办法
4681
查看次数