Java Concurrency/Threads(1)

1. Process

  1. A process is a unit of execution that has its own memory space.
    Each instance of a JVM runs as a process (for most of JVMs).

  2. We use the terms process & application interchangeably.

  3. If multiple Java applications are running, each of them has its own memory space of heap. && Heap isn't shared between applications.

## 2. Thread

  1. A thread is a unit of execution within a process.

  2. Each process can have multiple threads.

  3. Every Java process (application) has at least one thread, the ** main thread **.

  4. Every Java process also has multiple ** system threads ** that handle tasks like memory management and I/O. Developers don't explicitly create and code those system threads.

  5. Our code runs on the main thread, which is created automatically by your Java program, or in other threads that we explicitly create.

3. Creating Threads

  1. Creating a thread doesn't require as many resources as creating a process.

  2. Every thread created by a process shares the process's memory and files.

4. Process Memory

  1. Each thread has a ** thread stack **, which is the memory that only that thread can access.

Summary

Every Java application runs as a single process, and each process can have multiple threads. Every process has a heap, and every thread has its own thread stack.

4. Concurrency

  1. Referring to an application doing more than one thing at a time. Actually, it means that progress can be made on more than one task.

Example. Let's say that an application wants to download data and draw a shape on the screen.

If it's a concurrent application, it can download a bit of data, then switch to drawing part of the shape, then switch back to downloading some more data, then switch back to drawing more of the shape, etc.

  1. Concurrency means that one task doesn't have to complete before another can start.

5. Create and enable a new Thread

a, by creating a Thread of subClass, overloaded Thread class

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello from main thread.");

        Thread anotherThread = new AnotherThread(); // new 一个新的Thread类,并赋值给Thread对象
        anotherThread.start(); // 调用Thread对象的start()方法,启用新线程

        System.out.println("Hello again from the main thread.");

    }
}
// override Thread Class
public class AnotherThread extends Thread {

    @Override
    public void run() {      // 线程中入口函数是run()
        System.out.println("Hello from another thread.");
    }
}

b, opening of the new thread class by anonymous class

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello from main thread.");
        
        new Thread() {
            System.out.println("Hello from anonymous class.");
        }.start();   // 调用Thread类的start()方法,启用线程

        System.out.println("Hello again from the main thread.");

    }
}

NOTE : After mian thread enable other threads, the order that they perform before agnostic

c, Runnable object Construction

import static com.guoqiang.ThreadColor.ANSI_RED;

public class MyRunnable implements Runnable {
    // 重载Runnable 类   「implements 和 extends 在override上的差别」
    @Override
    public void run() {
        System.out.println(ANSI_RED + "Hello from MyRunnable's implementation of run()");
    }
}

public class Main {

    public static void main(String[] args) {
        System.out.println(ANSI_PURPLE + "Hello from main thread.");

        Thread myRunnableThread = new Thread(new MyRunnable() {
            @Override
            public void run() {
                System.out.println(ANSI_RED + "Hello from anonlymous implementation of run()");;
            }
        });

        myRunnableThread.start();

        System.out.println(ANSI_PURPLE + "Hello again from the main thread.");

    }
}

VS Thread Runnable class of subClass class creates a new thread

  1. Developers generally use Runnable object way to start a new thread
  2. Thread class like workers, like Runnable class task, generally speaking, only one worker to run common tasks, establish an independent class implements Runnable to workers can be. (Related to the design concept)
    NOTE:
  3. Note that only a thread start function starts the thread.

6. Thread sleep

By calling thread's sleep (milliseconds) method, so that the thread periodically take a break.
This approach is likely to "throw InterruptedException exception must contain a call to sleep in a try / catch block."

        try {
            Thread.sleep(3000);
        } catch ( InterruptedException e) {
            System.out.println("Another thread woke me up");
        }

7. Interrupt

Guess you like

Origin www.cnblogs.com/isguoqiang/p/11463169.html