If the Java Virtual Machine (JVM) uses native threads, then the different threads will be able to execute in different processors in parallel on multiprocessors machines. However, under some JVMs and operating systems using native threads is not sufficient to allow the JVM access to multiple processors. This is the case when native threads are implemented using POSIX threads on lightweight processes (i.e. PTHREAD_SCOPE_PROCESS sopce scheduling), which is the case on most UNIX operating systems. In order to do provide access to multiple processors it is necessary to set the concurrency level to the number of processors or slightly higher. This can be achieved by setting the Java system property with the name defined by CONCURRENCY_PROP_NAME to some non-negative number. This will make use of the 'NativeServices' class and supporting native libraries. See 'NativeServices' for details. See 'CONCURRENCY_PROP_NAME' for the name of the property.
Initially the thread pool contains a user specified number of idle threads. Idle threads can be given a target which is run. While running the target the thread temporarily leaves the idle list. When the target finishes, it joins the idle list again, waiting for a new target. When a target is finished a thread can be notified on a particular object that is given as a lock.
Jobs can be submitted using Runnable interfaces, using the 'runTarget()' methods. When the job is submitted, an idle thread will be obtained, the 'run()' method of the 'Runnable' interface will be executed and when it completes the thread will be returned to the idle list. In general the 'run()' method should complete in a rather short time, so that the threds of the pool are not starved.
If using the non-asynchronous calls to 'runTarget()', it is important that any target's 'run()' method, or any method called from it, does not use non-asynchronous calls to 'runTarget()' on the same thread pool where it was started. Otherwise this could create a dead-lock when there are not enough idle threads.
The pool also has a global error and runtime exception condition (one for 'Error' and one for 'RuntimeException'). If a target's 'run()' method throws an 'Error' or 'RuntimeException' the corresponding exception condition is set and the exception object saved. In any subsequent call to 'checkTargetErrors()' the saved exception object is thrown. Likewise, if a target's 'run()' method throws any other subclass of 'Throwable' a new 'RuntimeException' is created and saved. It will be thrown on a subsequent call to 'checkTargetErrors()'. If more than one exception occurs between calls to 'checkTargetErrors()' only the last one is saved. Any 'Error' condition has precedence on all 'RuntimeException' conditions. The threads in the pool are unaffected by any exceptions thrown by targets.
The only exception to the above is the 'ThreadDeath' exception. If a target's 'run()' method throws the 'ThreadDeath' exception a warning message is printed and the exception is propagated, which will terminate the thread in which it occurs. This could lead to instabilities of the pool. The 'ThreadDeath' exception should never be thrown by the program. It is thrown by the Java(TM) Virtual Machine when Thread.stop() is called. This method is deprecated and should never be called.
All the threads in the pool are "daemon" threads and will automatically terminate when no daemon threads are running. @see NativeServices @see #CONCURRENCY_PROP_NAME @see Runnable @see Thread @see Error @see RuntimeException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|