Java学习-069-多线程02:实现 Runnable 接口
本文通过实现 Runnable 接口实现多线程,源代码如下所示:
package com.fanfengping.demo;import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Demo01Runnable implements Runnable{
private Thread t;
private String threadName;
Demo01Runnable(String tName) {
threadName = tName;
log.info("Create a Runnable : {}", threadName);
}
@Override
public void run() {
log.info("Running Runnable Thread : {}", threadName);
try {
for(int i = 3; i > 0; i--) {
log.info("Current Runnable name is {}, work batch : {}", threadName, i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
log.info("Runnable {} interrupted!", threadName);
e.printStackTrace();
}
log.info("Runnable {} exit", threadName);
}
public void start() {
log.info("Start Runnable : {}", threadName);
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}
测试源代码如下所示:
package com.fanfengping.demo;import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
public class Demo01RunnableTest {
@Test
public void test() throws InterruptedException {
Demo01Runnable t1 = new Demo01Runnable("Runnable-1");
Demo01Runnable t2 = new Demo01Runnable("Runnable-2");
Demo01Runnable t3 = new Demo01Runnable("Runnable-3");
Demo01Runnable t4 = new Demo01Runnable("Runnable-4");
Demo01Runnable t5 = new Demo01Runnable("Runnable-5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
// 延长一定时间,否则test执行完成后,子线程会终止
Thread.sleep(10000);
log.info("Test Cancel");
}
public static void main(String args[]) {
Demo01Runnable t1 = new Demo01Runnable("Runnable 1");
Demo01Runnable t2 = new Demo01Runnable("Runnable 2");
Demo01Runnable t3 = new Demo01Runnable("Runnable 3");
Demo01Runnable t4 = new Demo01Runnable("Runnable 4");
Demo01Runnable t5 = new Demo01Runnable("Runnable 5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
测试源代码中通过 TestNG 测试方法和 main 方法进行测试,使用 TestNG 进行测试时,若未添加休眠延时 Thread.sleep(10000),在测试方法执行完成后,启动的子线程也就随之结束了,此时子线程并未完成执行完成,若需要完全执行完成,需要添加 Thread.sleep,并设置休眠时间大于所有子线程执行时间总和,才可使得子线程得以成功执行完成。
@Test 方法未设置休眠时,输出的日志信息如下所示:
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable-1[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable-2
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable-3
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable-4
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable-5
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable-1
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable-2
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable-3
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable-4
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable-5
[main] INFO com.fanfengping.demo.Demo01RunnableTest - Test Cancel
[Runnable-1] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable-1
[Runnable-1] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable-1, work batch : 40
[Runnable-5] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable-5
[Runnable-4] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable-4
[Runnable-4] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable-4, work batch : 40
[Runnable-2] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable-2
[Runnable-3] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable-3
[Runnable-5] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable-5, work batch : 40
[Runnable-3] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable-3, work batch : 40
[Runnable-2] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable-2, work batch : 40
使用 main 方法或 @Test 方法启用休眠时,输出的结果如下所示:
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable 1[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable 2
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable 3
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable 4
[main] INFO com.fanfengping.demo.Demo01Runnable - Create a Runnable : Runnable 5
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable 1
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable 2
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable 3
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable 4
[Runnable 2] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable 2
[Runnable 1] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable 1
[Runnable 1] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 1, work batch : 3
[Runnable 2] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 2, work batch : 3
[main] INFO com.fanfengping.demo.Demo01Runnable - Start Runnable : Runnable 5
[Runnable 4] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable 4
[Runnable 4] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 4, work batch : 3
[Runnable 3] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable 3
[Runnable 3] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 3, work batch : 3
[Runnable 5] INFO com.fanfengping.demo.Demo01Runnable - Running Runnable Thread : Runnable 5
[Runnable 5] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 5, work batch : 3
[Runnable 5] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 5, work batch : 2
[Runnable 4] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 4, work batch : 2
[Runnable 3] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 3, work batch : 2
[Runnable 2] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 2, work batch : 2
[Runnable 1] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 1, work batch : 2
[Runnable 2] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 2, work batch : 1
[Runnable 1] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 1, work batch : 1
[Runnable 5] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 5, work batch : 1
[Runnable 3] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 3, work batch : 1
[Runnable 4] INFO com.fanfengping.demo.Demo01Runnable - Current Runnable name is Runnable 4, work batch : 1
[Runnable 2] INFO com.fanfengping.demo.Demo01Runnable - Runnable Runnable 2 exit
[Runnable 4] INFO com.fanfengping.demo.Demo01Runnable - Runnable Runnable 4 exit
[Runnable 1] INFO com.fanfengping.demo.Demo01Runnable - Runnable Runnable 1 exit
[Runnable 3] INFO com.fanfengping.demo.Demo01Runnable - Runnable Runnable 3 exit
[Runnable 5] INFO com.fanfengping.demo.Demo01Runnable - Runnable Runnable 5 exit
以上是 Java学习-069-多线程02:实现 Runnable 接口 的全部内容, 来源链接: utcz.com/z/392063.html