The initial JUC

What is JUC

JUC isjava.util .concurrentShort for package, this is a toolkit for handling threads.

Processes and threads

2.1 Processes and Threads

  • Process: A process, a collection of programs such as Qq. exe, a process can often contain many threads, at least one
  • Thread: The smallest unit in which an operating system can schedule operations

Java has two default threads, main and GC

Can Java really start threads?

Java itself does not have permission to start a thread. The start() source code shows that the local method is called, that is, the underlying C++ is called

public synchronized void start(a) {
        /** * 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. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if(! started) { group.threadStartFailed(this); }}catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */}}}//native is a native method that calls the underlying C++. Java runs on a virtual machine and cannot call hardware
    private native void start0(a);
Copy the code

2.2 Parallelism and Concurrency

  • Concurrency (multiple threads working on the same resource simultaneously)

If the CPU has only one core, only one instruction can be processed at a time, and concurrency can be achieved by fast switching to virtual multiple threads.

  • Parallelism (multiple people walking together) : Parallelism improves efficiency, using thread pools

CPU multi-core, multiple threads can be executed simultaneously

  • How to obtain the number of system CPU cores:
public class Test1 {
    public static void main(String[] args) {
        // Get the CPU cores
        //CPU intensive, IO intensiveSystem.out.println(Runtime.getRuntime().availableProcessors()); }}Copy the code

The essence of Concurrent programming: Make full use of CPU Resources Why concurrent programming: THE speed of CPU/ memory /IO devices varies greatly. In order to take advantage of the high performance of CPU, the operating system adds processes/threads.

2.3 Java threads have several states

Through the source code can know that the thread has six states

Thread.State.NEW;
public enum State {
        /** * Thread state for a thread which has not yet started. */
        NEW,		// The thread is new
        RUNNABLE,	// The thread runs
        BLOCKED,	// The thread is blocked
        WAITING,	// Wait, wait, wait
        TIMED_WAITING,	// Wait for timeout
        TERMINATED;		/ / termination
}
Copy the code

2.4 Differences between Wait /sleep

  • From different classes
wait ==> Object  
sleep ==> Thread
Copy the code
  • About lock release
Wait releases the lock, sleep does not release the lockCopy the code
  • The scope of use is different
Wait: objects that must be used in synchronized blocks and must be able to waitCopy the code
  • Whether an exception needs to be caught
Wait does not need to catch exceptions; Sleep requires that the process catch InterruptedException(checked Exception) and throw an exception if it is interrupted during sleep.Copy the code
public static void main(String[] args){
        Thread a = new Thread(){
            @Override
            public void run(a){
                try {
                    System.out.println("Thread in execution");
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("Dormant process interrupted"); }}}; a.start(); a.interrupt(); }Copy the code
Is the process of executing thread dormancy is interrupted. Java lang. InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at gdut.hzh.demo01.Test1$1.run(Test1.java:17)Copy the code