java concurrency_Java Concurrency/Threads(1)

1. Process

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

We use the terms process & application interchangeably.

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

2. Thread

A thread is a unit of execution within a process.

Each process can have multiple threads.

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

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.

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

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

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

4. Process Memory

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

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.

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

Java provides ** thread-related classes** so that we can create Java concurrent applications.

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类 创建新线程

开发人员一般采用Runnable对象的方式启动新的线程

Thread类 好比工人, Runnable类 好比任务,一般情况而言,只需要一个工人来运行一般的任务,建立实现Runnable的独立类给工人就可以。(与设计概念相关)

NOTE:

注意只能用线程的start函数启动线程。

6. 线程sleep

通过调用线程的sleep(毫秒)方法,让线程周期性的休息一下。

这个方法可能会「抛出InterruptedException异常,对sleep的调用必须包含在try/catch块中」

try {

Thread.sleep(3000); // sleep方法是一个static方法

} catch ( InterruptedException e) {

System.out.println("Another thread woke me up");

}

时间过后,线程变为可执行状态,等待被调度器挑出来执行。

7. setName 和 Thread.currentThread().getName()

借助这两个方法可以设置线程的名字和获取当前线程的名字。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值