java.util.concurrent

JavaFX thread synchronization with Java thread

孤街醉人 提交于 2019-12-01 23:24:21
Is there a way to synchronize a JavaFX Platform thread and a standard Java thread? Currently, when triggered, the JavaFX thread fires before the standard Java thread has finished adding all of the images to the observable list, and so the imageList is updated with a blank collection. private final TilePane imageList; final File[] files = new File(dir).listFiles(); final List<ImageView> views = FXCollections.observableArrayList(); new Thread() { @Override public void run() { for (final File file : files) { if (Utils.fileIsImage(file) && !file.isDirectory()) { ImageView view = new ImageView(new

Ensure that a task is interruptible

 ̄綄美尐妖づ 提交于 2019-12-01 21:38:13
How to ensure that my tasks are responsive to interruption when I call Future.cancel() ? ExecutorService executor = Executors.newSingleThreadExecutor(); Future<Boolean> future = executor.submit(task); try { future.get(timeout, timeoutUnit); } catch (TimeoutException e) { future.cancel(true); } How to ensure that my tasks are responsive to interruption when I call Future.cancel()? Calling future.cancel(...) will stop the task it has not been run yet. If it is being run then if you use future.cancel(true) it will interrupt the running thread. To stop the thread you need to test the thread

ForkJoinPool resets thread interrupted state

跟風遠走 提交于 2019-12-01 19:05:44
I just noticed the following phenomena when cancelling a Future returned by ForkJoinPool . Given the following example code: ForkJoinPool pool = new ForkJoinPool(); Future<?> fut = pool.submit(new Callable<Void>() { @Override public Void call() throws Exception { while (true) { if (Thread.currentThread().isInterrupted()) { // <-- never true System.out.println("interrupted"); throw new InterruptedException(); } } } }); Thread.sleep(1000); System.out.println("cancel"); fut.cancel(true); The program never prints interrupted . The docs of ForkJoinTask#cancel(boolean) say: mayInterruptIfRunning -

How to wait for data with ReentrantReadWriteLock?

一曲冷凌霜 提交于 2019-12-01 18:18:50
It is said, that ReentrantReadWriteLock is intended for one writer and multiple readers. Nevertheless, readers should wait until some data is present in the buffer. So, what to lock? I created concurrency objects like follows: private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); protected final Lock readLock = rwl.readLock(); protected final Lock writeLock = rwl.writeLock(); protected final Condition hasData = writeLock.newCondition(); now in write method I do: writeLock.lock(); // writing first portion and updating variables hasData.signalAll(); // if required then writing

How to wait for data with ReentrantReadWriteLock?

♀尐吖头ヾ 提交于 2019-12-01 18:14:37
问题 It is said, that ReentrantReadWriteLock is intended for one writer and multiple readers. Nevertheless, readers should wait until some data is present in the buffer. So, what to lock? I created concurrency objects like follows: private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); protected final Lock readLock = rwl.readLock(); protected final Lock writeLock = rwl.writeLock(); protected final Condition hasData = writeLock.newCondition(); now in write method I do: writeLock

Using multiple threads to print statements sequentially

强颜欢笑 提交于 2019-12-01 10:54:37
I am trying to print numbers from 1 to 10 using three threads. thread 1 prints 1, 2 prints 2, 3 prints 3, 4 is printed by thread 1 again and so on. I have created a shared printer resource that helps those threads to print number. But I am getting confused as how can i make the number to be visible by all threads. The problem is eachthread is seeing their own copy of number while I need the same number to be shared by all threads. I am trying to create this example for learning purposes. I have seen other pages on SO that had same kind of problem but I am not able to get the concept. Any help

Using multiple threads to print statements sequentially

江枫思渺然 提交于 2019-12-01 09:28:02
问题 I am trying to print numbers from 1 to 10 using three threads. thread 1 prints 1, 2 prints 2, 3 prints 3, 4 is printed by thread 1 again and so on. I have created a shared printer resource that helps those threads to print number. But I am getting confused as how can i make the number to be visible by all threads. The problem is eachthread is seeing their own copy of number while I need the same number to be shared by all threads. I am trying to create this example for learning purposes. I

Restricting thread count and Java concurrency

允我心安 提交于 2019-12-01 09:21:10
I couldn't find an example of this specific case using the latest JAVA concurrent routines. I plan to use threads to process items from an open queue which may contain 0 to thousands requests. I want to restrict so at at any given time there be no less than 0 and no more than say 10 threads handling queue items. Is there a Java concurrent process geared towards this specific type of case? I think a thread pool is what you are looking for. Take a look at ExecutorService and Executors. ExecutorService : http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html Executors

Java Concurrent Object Pool?

99封情书 提交于 2019-12-01 08:01:17
I tried to integrate an external non-thread-safe library to my web project; I found out that it's too expensive to create an instance of this object for each client thread. As a result, I would like to create an object pool which has the following property. Dynamic object creation, the objects in the pool are dynamically created instead of creating them in the constructor. The pool is initially empty, and when a client thread acquires a resource object, the pool can create a new resource on demand. Once the numbers of created objects are reached the size of the pool; then the new client

Java Concurrent Object Pool?

孤街醉人 提交于 2019-12-01 07:12:19
问题 I tried to integrate an external non-thread-safe library to my web project; I found out that it's too expensive to create an instance of this object for each client thread. As a result, I would like to create an object pool which has the following property. Dynamic object creation, the objects in the pool are dynamically created instead of creating them in the constructor. The pool is initially empty, and when a client thread acquires a resource object, the pool can create a new resource on