Home > Java > javaTutorial > How to implement Runnable interface in Java?

How to implement Runnable interface in Java?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-21 14:43:08
forward
1793 people have browsed it

But the Java Runnable interface does not have any support for threads. We must also create an instance of the Thread class, which is achieved through the constructor public Thread (Runnable target) of the Thread class; The following is an example:

public class MyThread implements Runnable {   int count=1, number;   public MyThread(int num) {   numnumber = num;   System.out.println("创建线程 " + number);   }   public void run() {   while(true) {   System.out.println("线程 " + number + ":计数 " + count);   if(++count== 6) return;   }   }   public static void main(String args[]) {   for(int i = 0; i < 5; i++)   new Thread(new MyThread(i+1)).start();   }   }
Copy after login

Using the Java Runnable interface to implement multi-threading allows us to contain all code in one class, which is conducive to encapsulation. Let us study some issues in the use of multi-threading.

The above is the detailed content of How to implement Runnable interface in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template