JUC programming


What is JUC

JUC programming

refer tojava.util.concurrenttool kit

Business: common thread code thread
Runnable has no return value and its efficiency is relatively lower than that of callable

Threads and processes

Thread, process

  • Process: a program, such as qq.exe, a collection of programs;
    A process can often contain multiple threads, at least one
    Java default collection thread? 2 main, GC
  • Thread: open a process typora, write and save automatically (thread is responsible)
    For Java: thread, runnable, callable

Can Java really start threads?can’t get started

public synchronized void start() {
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         * A zero status value corresponds to state "NEW".
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */

        boolean started = false;
        try {
            started = true;
        } finally {
            try {
                if (!started) {
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
//Local methods, the underlying C + +, Java can not directly operate the hardware
    private native void start0();

Concurrent, parallel

Concurrent programming: concurrent, parallel

  • Concurrency (multiple threads operate on the same resource)
    CPU core, simulating multiple threads, fast alternation
  • Parallel, multi-core CPU, multiple threads can execute at the same time; Thread pool
    public class Test1 {
      public static void main(String[] args) {
          //Gets the number of cores of the CPU
          //CPU intensive, IO intensive

    The essence of concurrent programming: making full use of CPU resources

Thread state





//Wait, die, etc

//Timeout wait



1. From different classes
wait => Object
sleep => Thread

2. About lock release
Wait will release the lock. Sleep will not release the lock when she falls asleep

3. The scope of use is different
Wait must be in the synchronization code block
Sleep can sleep anywhere

4. Need to catch exceptions
Wait does not need to catch exceptions
Sleep must catch an exception

This work adoptsCC agreement, reprint must indicate the author and the link to this article