


Detailed explanation of the difference between Thread and runnable in Java multithreading
This article mainly introduces relevant information about the difference between java multi-threaded Thread and runnable. There are two methods for java thread to inherit the thread class and implement the runnable interface. Examples are provided below to help everyone understand. Friends in need can refer to it
The difference between multi-threaded Thread and runnable
There are two ways to implement multi-threading in java: inheriting the Thread class and implementing the runnable interface
1, inherit the Thread class and override the parent class run() method
public class thread1 extends Thread { public void run() { for (int i = 0; i < 10000; i++) { System.out.println("我是线程"+this.getId()); } } public static void main(String[] args) { thread1 th1 = new thread1(); thread1 th2 = new thread1(); th1.run(); th2.run(); } }
The run() method is just an ordinary method, It is executed sequentially, that is, th2.run() is executed only after th1.run() is completed, so only one main thread is used for writing. Multi-threading is meaningless, so the start() method should be used to start the thread. The start() method will automatically call the run() method. The above code is changed to:
public class thread1 extends Thread { public void run() { for (int i = 0; i < 10000; i++) { System.out.println("我是线程"+this.getId()); } } public static void main(String[] args) { thread1 th1 = new thread1(); thread1 th2 = new thread1(); th1.start(); th2.start(); } }
Start a new thread through the start() method. In this way, regardless of whether the run() method called by th1.start() has been executed, th2.start() will continue to be executed. If there is other code below, there is no need to wait for th2.start() to complete execution and continue execution. (The output thread ID is output randomly and alternately)
#2, implement the runnable interface
public class thread2 implements Runnable { public String ThreadName; public thread2(String tName){ ThreadName = tName; } public void run() { for (int i = 0; i < 10000; i++) { System.out.println(ThreadName); } } public static void main(String[] args) { thread2 th1 = new thread2("线程A"); thread2 th2 = new thread2("Thread-B"); th1.run(); th2.run(); } }
Like Thread's run method, Runnable's run is just a normal method. In the main method, th2.run() must wait for th1.run() to complete before it can be executed. The program only uses one thread. For multi-threading purposes, you must also use Thread's start() method (runnable does not have a start method). The above code is modified to:
public class thread2 implements Runnable { public String ThreadName; public thread2(String tName){ ThreadName = tName; } public void run() { for (int i = 0; i < 10000; i++) { System.out.println(ThreadName); } } public static void main(String[] args) { thread2 th1 = new thread2("线程A"); thread2 th2 = new thread2("Thread-B"); Thread myth1 = new Thread(th1); Thread myth2 = new Thread(th2); myth1.start(); myth2.start(); } }
Summary: 2 ways to implement java multi-threading, runable is an interface, thread is a class, runnable Only one run method is provided. It is recommended to use runable to implement java multi-threading. No matter what, you will eventually need to use thread.start() to make the thread in a runnable state.
The above is the detailed content of Detailed explanation of the difference between Thread and runnable in Java multithreading. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

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

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

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


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!