[Thread] Single task flow in Java program. We implement each task in a relatively independent thread. main is the main thread
[Concurrency] Complete multiple tasks at the same time. The steps of program execution are all in order, but many times we need to process a problem concurrently instead of processing a problem in sequence
[Multi-threading] Threads are also regarded as objects, and multi-threading refers to multiple thread objects
[Classes that support threads in the API] java.lang.Thread. The object of the Thread class is the thread object
Exercise 1. Initialize the thread object and print the thread
package pkg3;public class test3 implements Runnable{ Thread th1; public test3() {//2 th1=new Thread(this);//2-1初始化了线程对象 th1.start();//2-2启动了线程对象,自动调用run方法 } public static void main(String[] args){ new test3();//1.从主线程开始,调用构造方法 }@Overridepublic void run() {//3 // TODO Auto-generated method stub System.out.println("线程运行了");//3-1打印线程,“运行了”} }
Exercise 2. The life cycle of the thread: new, runnable, not runnable, dead
package pkg3;public class test3 implements Runnable{ Thread th1; public test3() {//2 th1=new Thread(this);//2-1初始化了线程对象 th1.start();//2-2启动了线程对象,自动调用run方法 } public static void main(String[] args){ new test3();//1.从主线程开始,调用构造方法 }@Overridepublic void run() {//3 // TODO Auto-generated method stub while(true) { System.out.println("线程运行了");//3-1打印线程,“运行了” try { th1.sleep(500);//强制睡眠500毫秒,进入非运行状态not runnable(睡眠、堵塞、排队) } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Exercise 3, multi-threading
package pkg3;public class test3 implements Runnable{ Thread th1,th2; public test3() {//2 th1=new Thread(this);//2-1初始化了线程对象 th2=new Thread(this); th1.start();//2-2启动了线程对象,自动调用run方法 th2.start(); } public static void main(String[] args){ new test3();//1.从主线程开始,调用构造方法 }@Overridepublic void run() {//3 // TODO Auto-generated method stub /*while(true) { System.out.println("线程运行了");//3-1打印线程,“运行了” try { th1.sleep(500);//强制睡眠500毫秒,进入非运行状态not runnable(睡眠、堵塞、排队) } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ Thread th=Thread.currentThread();//这个方法可以判断进入run方法的线程对象 if(th==th1) { System.out.println("线程1运行了"); } if(th==th2) { System.out.println("线程2运行了"); } } }
Related recommendations:
Detailed explanation of java threads and the difference between threads and processes
The above is the detailed content of JAVA--Threads and multi-threading Detailed graphic explanation. For more information, please follow other related articles on the PHP Chinese website!

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
