Java線程基本使用之如何實(shí)現(xiàn)Runnable接口
1. 實(shí)現(xiàn) Runnable 接口
說明
- java是單繼承的,在某些情況下一個(gè)類可能已經(jīng)繼承了某個(gè)父類,這時(shí)在用繼承Thread類方法來創(chuàng)建線程顯然不可能了。
- java設(shè)計(jì)者們提供了另外一個(gè)方式創(chuàng)建線程,就是通過實(shí)現(xiàn)Runnable接口來創(chuàng)建線程
應(yīng)用案例
- 請(qǐng)編寫程序,該程序可以每隔
1秒。在控制臺(tái)輸出“你好,兮動(dòng)人”,當(dāng)輸出10次后,自動(dòng)退出。 - 使用實(shí)現(xiàn)
Runnable接口的方式實(shí)現(xiàn)。
public class Thread02 {
public static void main(String[] args) {
Dog dog = new Dog();
//dog.start(); //這里不能調(diào)用 start 方法
//創(chuàng)建了 Thread 對(duì)象,把 dog 對(duì)象(實(shí)現(xiàn)了 Runnable ),放入了 Thread
Thread thread = new Thread(dog);
thread.start();
}
}
class Dog implements Runnable { //通過實(shí)現(xiàn)Runnable接口來實(shí)現(xiàn)
int count = 0;
@Override
public void run() { //普通方法
while (true) {
System.out.println("你好,兮動(dòng)人-" + (++count) + Thread.currentThread().getName());
//休眠1秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 10) {
break;
}
}
}
}

這里底層使用了設(shè)計(jì)模式【代理模式】=> 代碼模擬實(shí)現(xiàn)Runnable接口 開發(fā)線程的機(jī)制。
public class Thread02 {
public static void main(String[] args) {
Tiger tiger = new Tiger();
ThreadProxy threadProxy = new ThreadProxy(tiger);
threadProxy.start();
}
}
class Animal {}
class Tiger extends Animal implements Runnable {
@Override
public void run() {
System.out.println("老虎...");
}
}
//線程代理類,模擬了一個(gè)極簡(jiǎn)的Thread類
class ThreadProxy implements Runnable { //可以把 Proxy 類當(dāng)做 Thread
private Runnable target = null; // 屬性類型 是 Runnable
@Override
public void run() {
if (target != null) {
target.run();//動(dòng)態(tài)綁定(運(yùn)行類型是Tiger)
}
}
public ThreadProxy(Runnable target) {
this.target = target;
}
public void start() {
start0();//這個(gè)方法是真正實(shí)現(xiàn)多線程的方法
}
public void start0() {
run();
}
}

2. 線程使用應(yīng)用案例–多線程執(zhí)行
請(qǐng)編寫一個(gè)程序,創(chuàng)建兩個(gè)線程,一個(gè)線程每隔1秒輸出“hello,world”,輸出10次,退出,另一個(gè)線程每隔1秒輸出“hi”,輸出5次退出。
public class Thread03 {
public static void main(String[] args) {
T1 t1 = new T1();
T2 t2 = new T2();
Thread thread1 = new Thread(t1);
thread1.start();
Thread thread2 = new Thread(t2);
thread2.start();
}
}
class T1 implements Runnable {
int count = 0;
@Override
public void run() {
while (true) {
System.out.println("hello world" + (++count));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 10) {
break;
}
}
}
}
class T2 implements Runnable {
int count = 0;
@Override
public void run() {
while (true) {
System.out.println("hi" + (++count));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 5) {
break;
}
}
}
}

3. 如何理解線程


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven POM文件配置打造高效項(xiàng)目管理的完整指南
文章瀏覽閱讀1.6k次,點(diǎn)贊32次,收藏21次。本文全面解析 Maven 的 POM 文件配置,涵蓋項(xiàng)目信息、依賴管理、構(gòu)建配置等內(nèi)容。提供清晰示例與注釋,附思維導(dǎo)圖,助你快速掌握 Maven 項(xiàng)目管理。_maven pom2025-08-08
簡(jiǎn)單講解Android開發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法
這篇文章主要介紹了Android開發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法,包括事件偵聽器等安卓開發(fā)中常用的接口的基本使用方法,需要的朋友可以參考下2015-12-12
Java?Web防止同一用戶同時(shí)登錄幾種常見的實(shí)現(xiàn)方式
在JavaWeb開發(fā)中,實(shí)現(xiàn)同一賬號(hào)同一時(shí)間只能在一個(gè)地點(diǎn)登錄的功能,主要目的是為了增強(qiáng)系統(tǒng)的安全性,防止用戶賬戶被他人惡意登錄或同時(shí)在多個(gè)設(shè)備上使用,這篇文章主要給大家介紹了關(guān)于Java?Web防止同一用戶同時(shí)登錄幾種常見的實(shí)現(xiàn)方式,需要的朋友可以參考下2024-08-08
java swing實(shí)現(xiàn)QQ賬號(hào)密碼輸入框
這篇文章主要為大家詳細(xì)介紹了Java swing實(shí)現(xiàn)QQ賬號(hào)密碼輸入框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
源碼解讀Spring-Integration執(zhí)行過程
Spring-Integration基于Spring,在應(yīng)用程序中啟用了輕量級(jí)消息傳遞,并支持通過聲明式適配器與外部系統(tǒng)集成,今天主要是看個(gè)簡(jiǎn)單的hello word進(jìn)來分析下整個(gè)執(zhí)行過程,感興趣的朋友一起看看吧2021-06-06
springboot 自定義LocaleResolver實(shí)現(xiàn)切換語言
我們?cè)谧鲰?xiàng)目的時(shí)候,往往有很多項(xiàng)目需要根據(jù)用戶的需要來切換不同的語言,使用國(guó)際化就可以輕松解決。這篇文章主要介紹了springboot 自定義LocaleResolver切換語言,需要的朋友可以參考下2019-10-10

