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. 创建并启用新的Thread

a、通过创建Thread的subClass,重载Thread类

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、 通过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:mian线程启用其它线程之后,他们之前执行的先后顺序不可知

c、构建Runnable对象

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.");

    }
}

Runnable类 VS Thread的subClass类 创建新线程

  1. 开发人员一般采用Runnable对象的方式启动新的线程
  2. Thread类 好比工人, Runnable类 好比任务,一般情况而言,只需要一个工人来运行一般的任务,建立实现Runnable的独立类给工人就可以。(与设计概念相关)
    NOTE:
  3. 注意只能用线程的start函数启动线程。

6. 线程sleep

通过调用线程的sleep(毫秒)方法,让线程周期性的休息一下。
这个方法有可能会「抛出InterruptedException异常,对sleep的调用必须包含在try/catch块中」

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

7. Interrupt

猜你喜欢

转载自www.cnblogs.com/isguoqiang/p/11463169.html