


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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
