初入多线程示例展示--Runner
利用多线程演示多人赛跑过程:
RunnerThread.java
package thread; /** * 所有的人来参加赛跑比赛 ,使用线程 那么 写100个线程? * 新建一个选手的线程 * 1.选手名字 * 2.线程名字 * @author superdrew * */ public class RunnerThread extends Thread{ private String runnerName; //选手名字 private String threadName; //线程名字 public RunnerThread(String runnerName,String threadName){ super(threadName); //将线程名传值给父类 this.runnerName = runnerName; } public void run() { while(true){ System.out.println(this.runnerName+"领先了... "+"当前线程:"+this.getName()); } } }
测试线程RunnerGame.java
package thread; /** * 不同的人来参加比赛,用的是一个线程的类,产生不同的线程对象 * 跑步比赛 * @author superdrew * * 1.继承Thread * 2.实现Runnable接口 通过Thread.start(); * run是 线程体 */ public class RunnerGame { public static void main(String[] args) { //选手来了 RunnerThread rt1 = new RunnerThread("Mark", "Mark_Thread"); RunnerThread rt2 = new RunnerThread("Bob", "Bob_Thread"); RunnerThread rt3 = new RunnerThread("Lily", "Lily_Thread"); RunnerThread rt4 = new RunnerThread("Geny", "Geny_Thread"); RunnerThread rt5 = new RunnerThread("King", "King_Thread"); //开跑 rt1.start(); rt2.start(); rt3.start(); rt4.start(); rt5.start(); } }