并发管理机制(如wait/notify和lock/condition)似乎受到虚假唤醒的影响.开发人员通过重新检查条件确实已经改变来满足那些意外的唤醒.
在CountDownLatch中,虚假的唤醒是一个问题吗?
我查看了代码,一切都是int - 传递给CountDownLatch构造函数的参数是int,Sync中的变量是int,Sync.getCount()的返回类型是int.但CountDownLatch.getCount()返回一个长?想知道为什么.
在文档中CountDownLatch我看到类似的东西:
public void run() {
try {
startSignal.await();
doWork();
doneSignal.countDown();
} catch (InterruptedException ex) {} // return;
}
这里startSignal和doneSignal是CountDownLatch对象.
文档没有提到关于类是否是线程安全的.
我正在寻找创造角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
我使用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(...)在同一个对象上超时,也能以上述方式使用第二个等待(...)吗?
在分析其中一个生产环境的日志时,我在倒计时锁存器等待()上看到一个处于“等待”状态的线程
...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) 我创建了一个示例,我想知道如何使用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
假设我有以下类定义,当一个线程想要为多个(可能)等待线程设置 a 时:
\n\npublic 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}\nRun Code Online (Sandbox Code Playgroud)\n\n在我看来, a 需要是易失性的,但我不确定\xe2\x80\xa6 有人可以分享一下为什么(如果有的话)需要围绕 getA 进行额外的同步,或者 a 需要是易失性的吗?
\n我意识到类似的问题已被问到,但我是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) 我有以下程序,我正在使用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
countdownlatch ×10
java ×7
android ×2
angular ×1
asynchronous ×1
callable ×1
concurrency ×1
countdown ×1
javascript ×1
retrofit2 ×1
runnable ×1