草庐IT

thread-synchronization

全部标签

java - Clojure STM ( dosync ) x Java 同步块(synchronized block)

ClojureSTM(dosync)方法和Java同步块(synchronizedblock)有什么区别?我正在阅读下面来自“sleep的理发师”问题的代码。(http://www.bestinclass.dk/index.clj/2009/09/scala-vs-clojure-round-2-concurrency.html)(defnthe-shop[a](print"[k]enteringshop"a)(dosync(if(为了避免竞争条件,使用了dosync,所以我问自己“与Java同步块(synchronizedblock)有什么区别(STM)”?它会阻止这个关键代码吗?提

java.util.concurrent 与 Boost Threads 库

BoostThread库与java.util.concurrent库相比如何?性能至关重要,因此我更愿意继续使用C++(尽管现在Java快多了)。鉴于我必须用C++编写代码,存在哪些库可以使线程处理变得简单且不易出错。我最近听说,从JDK1.5开始,Java内存模型已更改以解决一些并发问题。C++怎么样?上一次用C++进行多线程编程是在3-4年前,当时我使用的是pthreads。虽然,我不想再将它用于大型项目。我所知道的唯一其他选择是BoostThreads。但是,我不确定它是否好。我听说过有关java.util.concurrent的好消息,但对Boost线程还一无所知。

java - 在 Java 的循环中使用 Thread.sleep() 定期做某事是否可以?

我读过一些帖子说在循环中调用Thread.sleep()是有问题的,并且是一个严重的性能问题。但在某些情况下,这似乎是最自然的做法。例如,如果我希望我的应用程序每3分钟执行一次操作(假设它是一个自动保存)publicvoidstartAutosaveLoop(){stop=false;newThread(newRunnable(){@Overridepublicvoidrun(){while(!stop){Thread.sleep(T*1000);if(!stop){//dosomething}}}}).start();}有更好的方法吗?这种情况有问题吗?

java - Thread.currentThread() 是如何工作的?

Thread.currentThread()是一个static方法,它提供对当前正在执行的Thread的引用(基本上是对“this”线程的引用)。在静态方法中访问非静态成员(尤其是this)在Java中是不可能的,所以currentThread()是一个本地方法。currentThread()方法在幕后是如何工作的? 最佳答案 (basicallyareferenceto'this'thread)此处不涉及this引用。您正在将一个线程混为native资源,这意味着执行线程;和Thread,这是一个Java类。线程代码不在Threa

没有堆栈的 Java "Thread-2"会阻止终止

我有一个非常复杂的Java程序,它不会终止。Eclipse调试器显示一个可以暂停的线程,但没有堆栈跟踪。它被称为“Thread-2”。此线程的jstack-l输出是:"Thread-2"#17prio=5os_prio=0tid=0x00007f1268002800nid=0x3342runnable[0x0000000000000000]java.lang.Thread.State:RUNNABLELockedownablesynchronizers:-None我在Thread.start()中添加了断点,但找不到名为“Thread-2”的线程。该线程仅在创建两个“AWT-Event

java - 为什么是 "Multiplexed, non-blocking I/O, [..] much more scalable than thread-oriented, blocking I/O"?

我正在阅读JDK7文档(here)中有关channel的内容,并偶然发现了这个:Multiplexed,non-blockingI/O,whichismuchmorescalablethanthread-oriented,blockingI/O,[...]是否有关于为什么会这样的简单解释? 最佳答案 因为线程堆栈通常比支持异步I/O连接所需的数据结构大得多。此外,调度数千个线程效率低下。 关于java-为什么是"Multiplexed,non-blockingI/O,[..]muchmo

java - 如何让特定线程成为下一个进入同步块(synchronized block)的线程?

我在面试中被问到这个问题。Therearefourthreadst1,t2,t3andt4.t1isexecutingasynchronizedblockandtheotherthreadsarewaitingfort1tocomplete.Whatoperationwouldyoudo,sothatt3executesaftert1.我回答说join方法应该可以解决问题,但看起来这不是正确的答案。他给出的原因是,join方法和setPriority方法不适用于等待状态的线程。我们能做到吗?如果是,如何? 最佳答案 您可以使用锁和条

java - 始终调用 Thread.currentThread().interrupt();捕获 InterruptedException 时?

ThisIBMdeveloperWorksarticle状态:“Theonetimeitisacceptabletoswallowaninterruptiswhenyouknowthethreadisabouttoexit.ThisscenarioonlyoccurswhentheclasscallingtheinterruptiblemethodispartofaThread,notaRunnable[…]”.我现在总是为我的线程实现Runnable。像这样提供Runnable实现:publicclassView()implementsRunnable{@Overloadpublic

Error: A component suspended while responding to synchronous input...

解决报错:Theaboveerroroccurredinthecomponent:外层添加Suspense即可解决import{lazy,Suspense}from'react'importReactDOMfrom'react-dom/client'importAppfrom'./App'//这里路由采用了浏览器模式import{BrowserRouterasRouter}from'react-router-dom'//一定引入antd的样式import'antd/dist/reset.css';constroot=ReactDOM.createRoot(document.getElement

java - 我如何从 Java 中的内部 Thread Runnable 方法获取返回值?

如何使用isFinish()将Status分配给CallMe()以获得返回值true?publicstaticbooleanisFinish(){booleanStatus=false;newThread(newRunnable(){publicvoidrun(){/*Thisshellreturntrueorfalse*HowdoyoukeepitinStatus*/CallMe();}}).start();/*HowcanigetthetrueorfalseexactlyfromCallMe?here*/returnStatus;}publicstaticbooleanCallMe