Only the main function performed Thread.activeCount (), printing is: 2

When writing multi-threaded, and so you want other main thread execution (other threads to function as a digital inc + 1), the output value after completion of the final inc.

So wrote a cycle:

the while (Thread.activeCount ()>. 1) {
       System.out.println (Thread.activeCount ());
            to Thread.yield ();
        }
System.out.println (test.inc);
results into an infinite loop

So, main has written only under the code:

System.out.println (Thread.activeCount ()); // 2 results
for the following reasons:

// Get the java thread management the MXBean
        the ThreadMXBean tmxb = the ManagementFactory.getThreadMXBean ();
        // no need to obtain synchronization Monitor and synchronizer information, access to only a thread and the thread stack information
        ThreadInfo [] threadInfos = tmxb.dumpAllThreads (false , false);
        // traversing thread information, prints out the ID and name
        for (the ThreadInfo info: threadInfos) {
            System.out.println ( "[" info.getThreadId + () + "]" + info.getThreadName ());
        }
[. 6] the Ctrl-Break Monitor
[. 5] The Listener the Attach
[. 4] Signal the Dispatcher
[. 3] the Finalizer
[2] Handler Reference
[. 1] main
System.out.println (Thread.activeCount ());
. Thread.currentThread ()'s getThreadGroup (). List ();
2
java.lang.ThreadGroup [name = main, MaxPri = 10]
    The Thread [main, 5, main]
    the Thread [Monitor Ctrl-Break, 5, main]
can be found in the current thread group includes: main and Monitor Ctrl-Break

All multi-threaded test code is as follows:

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/ **
 * java.util.concurrent.atomic package available under some type atomic operation, i.e. the base data type is encapsulated to ensure that these operations are atomic operations
 * Atomic achieved using CAS atomic operation (Compare And swap), CAS actually using CMPXCHG instructions provided by the processor-implemented
 * CMPXCHG instructions and a processor to perform an atomic operation
 *
 * @author Jack
 * @Create 2018-05-04 12:12
 ** /
public class TestAtomicMain {

    private static final int THREAD_COUNT = 10;

    private static final int FOR_COUNT = 10;

    private static final int THREAD_ACTIVE_COUNT = 2;

    public AtomicInteger inc = new AtomicInteger();

    public void increase() {
        inc.getAndIncrement();
    }

    public static void main(String[] args) {

        final TestAtomicMain test = new TestAtomicMain();
        ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
        ThreadFactory namedThreadFactory = threadFactoryBuilder.setNameFormat("ThreadFactory-%d").build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT * 2,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024),
                namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < THREAD_COUNT; i++) {
            singleThreadPool.execute(() -> {
                for (int j = 0; j < FOR_COUNT; j++) {
                    test.increase();
                }
            });
        }
        SingleThreadPool.shutdown ();
        the while (Thread.activeCount ()> THREAD_ACTIVE_COUNT) {
            System.out.println ( "number of other active threads" + Thread.activeCount ());
            to Thread.yield ();
        }
        the System .out.println (test.inc);
    }
}
else active thread number 9
other active threads having 3
other active threads having 3
100
to achieve the desired effect


Original: https: //blog.csdn.net/ke_g3/article/details/80199209
 

Guess you like

Origin blog.csdn.net/weixin_44018338/article/details/91989252