概述
public class TestThread {
public static void main(String args[]) {
RunnableEat R1 = new RunnableEat( "eat");
Thread eat = new Thread(R1);
eat.start();
RunnableSleep R2 = new RunnableSleep( "sleep");
Thread sleep = new Thread(R2);
sleep.start();
}
}
class RunnableEat implements Runnable {
private String threadName;
RunnableEat( String name) {
threadName = name;
System.out.println("创建线程eat:" + threadName );
}
@Override
public void run() {
System.out.println("执行线程eat " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("ThreadEat: " + threadName + ", " + i);
// 让线程睡眠一会
Thread.sleep(50);
}
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.存在,但不执行?");
}
}
class RunnableSleep implements Runnable {
private String threadName;
RunnableSleep( String name) {
threadName = name;
System.out.println("创建线程sleep" + threadName );
}
@Override
public void run() {
System.out.println("执行线程sleep " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("线程sleep睡眠: " + threadName + ", " + i);
// 让线程睡眠一会
Thread.sleep(50);
}
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.终止");
}
System.out.println("Thread " + threadName + " exiting.存在,但不执行");
}
}
最后
以上就是霸气棒棒糖为你收集整理的多线程代码小案例的全部内容,希望文章能够帮你解决多线程代码小案例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复