Tag: java threads

java代码执行在没有断点和正常运行的调试中产生不同的结果。 ExecutorService坏了吗?

TL:DR ExecutorService executorService = Executors.newFixedThreadPool(8); 在debug中运行并发,但在正常运行时它启动并发,但后来在单线程中运行。 我有一些代码,我在ExecutorService启动了4个不同的任务。 其中两个任务应该几乎立即完成,另外两个应该运行一段时间。 这些任务在Future以秒为单位返回执行时间。 此代码负责任务执行和测量: public Future measure(int[] arr, ProcessIntArray processIntArray, ExecutorService es) { Callable task = () -> { long start = System.nanoTime(); processIntArray.process(arr); long end = System.nanoTime(); return (end – start) / 1000000000.0; }; return es.submit(task); } 稍后,在启动这些任务后,我将按照先前运行的执行时间顺序打印相同的输入大小。 Future bubbleSortTime = measure(bubbleSortArray, Solution::bubbleSort, executorService); Future insertionSortTime = measure(insertionSortArray, […]

在notifyAll()之前未被线程锁定的同步对象

我想要一个布尔值来通知系统某些特定服务启动的部分。 出于某些奇怪的原因,我收到错误java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll() 。 奇怪的是,notifyAll()位于一个synchronized块中,该块控制我调用notifyAll()的对象。 我的class级开头是这样的: public class MyService { public static Boolean notifier = Boolean.valueOf(false); @Override public void start() { synchronized (MyService.notifier) { MyService.notifier = Boolean.valueOf(true); MyService.notifier.notifyAll(); } } @Override public void stop() { synchronized (MyService.notifier) { MyService.notifier = Boolean.valueOf(false); MyService.notifier.notifyAll(); } } … } 我正在开发一个Android应用程序。 我认为它不应该影响任何东西,但是如果影响java的工作方式,我会用该注释补充问题。 如果对象锁定在同步块中,为什么会出现exception?

具有corePoolSize 0的ThreadPoolExecutor在任务队列已满之前不应执行任务

我正在经历Java Concurrency In Practice并且陷入8.3.1线程创建和拆解主题。 以下脚注警告将corePoolSize保持为零。 开发人员有时会试图将核心大小设置为零,这样工作线程最终会被拆除,因此不会阻止JVM退出,但这可能会导致在不使用a的线程池中出现一些看似奇怪的行为。用于工作队列的SynchronousQueue(如newCachedThreadPool所做的那样)。 如果池已经是核心大小,则ThreadPoolExecutor仅在工作队列已满时才创建新线程。 因此,在队列填满之前 , 提交给具有任何容量和核心大小为零的工作队列的线程池的任务将不会执行 ,这通常不是所希望的。 所以为了validation这一点,我写了这个程序,它不能像上面说的那样工作。 final int corePoolSize = 0; ThreadPoolExecutor tp = new ThreadPoolExecutor(corePoolSize, 1, 5, TimeUnit.SECONDS, new LinkedBlockingQueue()); // If the pool is already at the core size if (tp.getPoolSize() == corePoolSize) { ExecutorService ex = tp; // So tasks submitted to a thread pool with […]

通知方法如何工作

根据javadoc通知唤醒正在等待此对象监视器的单个线程。 如果任何线程正在等待此对象,则选择其中一个线程被唤醒。 选择是任意的,由实施决定。 线程通过调用其中一个wait方法等待对象的监视器。 我想知道notify如何实现这种行为。 在我读过的许多网站上发送信号但信号在这里意味着什么? notify是否直接向第一个等待线程发送信号,或者它向线程调度程序发送信号?

为什么Thread.sleep使用不好

为这个重复的问题道歉,但我还没有找到任何令人满意的答案。 大多数问题都有自己的特定用例: Java – thread.sleep的替代品 有没有更好的或替代的方法来跳过/避免在Java中使用Thread.sleep(1000)? 我的问题是非常通用的用例。 等待条件完成。 做一些操作。 检查条件。 如果条件不成立,请等待一段时间再次执行相同的操作。 例如,考虑一种通过调用其createAPI表来创建DynamoDB表的方法。 DynamoDB表需要一些时间才能变为活动状态,因此该方法会调用其DescribeTable API以定期轮询状态直到某个时间(假设5分钟 – 由于线程调度而导致的偏差是可接受的)。 如果表在5分钟内变为活动状态,则返回true,否则抛出exception。 这是伪代码: public void createDynamoDBTable(String name) { //call create table API to initiate table creation //wait for table to become active long endTime = System.currentTimeMillis() + MAX_WAIT_TIME_FOR_TABLE_CREATE; while(System.currentTimeMillis() < endTime) { boolean status = //call DescribeTable API to get […]

Java中的“实现Runnable”与“扩展线程”

从我在Java中使用线程的时间开始,我发现了这两种编写线程的方法: 使用implements Runnable : public class MyRunnable implements Runnable { public void run() { //Code } } //Started with a “new Thread(new MyRunnable()).start()” call 或者,使用extends Thread : public class MyThread extends Thread { public MyThread() { super(“MyThread”); } public void run() { //Code } } //Started with a “new MyThread().start()” call 这两个代码块有什么显着差异吗?