java.util.concurrent

Why cannot run() of Runnable throw checked Exceptions?

心已入冬 提交于 2019-12-03 10:07:32
According to section 6.3.2 of JCIP : Runnable is a fairly limiting abstraction; run can not return a value or throw checked exception . run() can not return a value since its return type is void but why can not it throw a checked exception ? Peter Lawrey It cannot throw a checked exception because it wasn't declared as throwing a checked exception from the first version and it is too dangerous to change it. Originally Runnable was only used in a wrapped Thread , and it was assumed the developer would want to catch all checked exceptions and handle them rather than logging them to System.err .

Why java.util.concurrent.atomic.AtomicBoolean is internally implemented with int?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 09:53:48
AtomicBoolean stores its value in: private volatile int value; Then, for example, extracting its value is done like this: public final boolean get() { return value != 0; } What is the reason behind it? Why boolean was not used? AFAIK, int is the smallest type CAS operations can be implemented across different machine types. Note: as object allocations are 8 byte aligned, using a smaller type wouldn't save any memory. This probably is to be able to base several of the Atomic classes on the same base ( Unsafe ), which uses integer and provides the compare and swap operation. Concurrency in

Can I use Callable threads without ExecutorService?

☆樱花仙子☆ 提交于 2019-12-03 07:15:32
问题 Can I use Callable threads without ExecutorService? We can use instances of Runnable and subclasses of Thread without ExecutorService and this code works normally. But this code works consistently: public class Application2 { public static class WordLengthCallable implements Callable { public static int count = 0; private final int numberOfThread = count++; public Integer call() throws InterruptedException { int sum = 0; for (int i = 0; i < 100000; i++) { sum += i; } System.out.println

Single threading a task without queuing further requests

与世无争的帅哥 提交于 2019-12-03 06:35:02
I have a requirement for a task to be executed asynchronously while discarding any further requests until the task is finished. Synchronizing the method just queues up the tasks and doesn't skip. I initially thought to use a SingleThreadExecutor but that queues up tasks as well. I then looked at the ThreadPoolExecutor but it reads the queue to get the task to be executed and therefore will have one task executing and a minimum of one task queued (the others can be discarded using ThreadPoolExecutor.DiscardPolicy). The only thing I can think off is to use a Semaphore to block the queue. I've

Can I use Callable threads without ExecutorService?

☆樱花仙子☆ 提交于 2019-12-02 20:48:23
Can I use Callable threads without ExecutorService? We can use instances of Runnable and subclasses of Thread without ExecutorService and this code works normally. But this code works consistently: public class Application2 { public static class WordLengthCallable implements Callable { public static int count = 0; private final int numberOfThread = count++; public Integer call() throws InterruptedException { int sum = 0; for (int i = 0; i < 100000; i++) { sum += i; } System.out.println(numberOfThread); return numberOfThread; } } public static void main(String[] args) throws

Multithreaded execution where order of finished Work Items is preserved

家住魔仙堡 提交于 2019-12-02 19:35:55
I have a flow of units of work, lets call them "Work Items" that are processed sequentially (for now). I'd like to speed up processing by doing the work multithreaded. Constraint: Those work items come in a specific order, during processing the order is not relevant - but once processing is finished the order must be restored. Something like this: |.| |.| |4| |3| |2| <- incoming queue |1| / | \ 2 1 3 <- worker threads \ | / |3| |2| <- outgoing queue |1| I would like to solve this problem in Java, preferably without Executor Services, Futures, etc., but with basic concurrency methods like wait(

When is CopyOnWriteArraySet useful to achieve thread-safe HashSet?

笑着哭i 提交于 2019-12-02 19:27:22
In Java , there is thread-safe version HashMap named ConcurrentHashMap and thread-safe version TreeMap named ConcurrentSkipListMap , but there is no ConcurrentHashSet for HashSet . Instead, there are usually 4 ways to use thread-safe Set : Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); Set<String> s = Collections.synchronizedSet(new HashSet<String>()); ConcurrentSkipListSet<E> CopyOnWriteArraySet<E> 1 use keySet() of ConcurrentHashMap to achieve both Set and thread-safe. 2 use synchronized way, it seems this way is not recommended. 3 is based on

AtomicBoolean, set flag once, necessary? Might a static boolean be ok?

情到浓时终转凉″ 提交于 2019-12-02 09:41:39
I am setting a flag which is set once by any thread that get to set it. All other threads will at various time, pretty often read this flag repeateadly. Right now I am using an AtomicBoolean, which works fine, but I know that if it is queried quite often it can be considerably slower than plain boolean, ( not sure if this is true ). Would it be thread safe to instead change this to a static boolean? Set the flag to true by whoever gets to do that, in fact all of them might be allowed to set the flag several times over. What I am concerned about is how quickly those reading the flag will be

What is happening underneath the Future.cancel(true)

十年热恋 提交于 2019-12-02 00:06:18
Suppose I have a Runnable instance: class MyTask implements Runnable { public void run() { //some heavy calculation which takes time Thread.sleep(5000) //rest code ... } } Then, I use ExecutorService to submit the above task: ExecutorService service = Executors.newFixedThreadPool(3); Future<?> task = service.submit(new MyTask()); Now, I can cancel the task by task.cancel(true); . What I have understood is that the task.cancel(true) will interrupt the working thread in which this task is running, like Thread.currentThread().interrupt() . But this only sets a flag to tell that the working thread

Java ServiceExecutor terminating condition

家住魔仙堡 提交于 2019-12-01 23:33:56
I'm new to java executor stuff. I'm using Java's ExecutorService to launch several threads to process data. Executor executor = Executors.newFixedThreadPool(poolSize); for(int i=0; i< 5;i++) executor.execute(new MyRunnable(i)); once the threads don't find data, they gracefully terminate. My question is what happens to the Executor when all the threads terminate, is it still running its master thread ? or it will terminate itself and whole application will finish gracefully? in case executor thread still runs, how can I let it terminate once all its child threads are done (poolSize number of