本帖最后由 黑马无敌 于 2015-6-27 22:34 编辑
- /**
- * 编写一个多线程程序,每隔30ms输出线程的名字(线程至少3个以上)【多线程
- * 思路: 1,创建一个线程类继承Thread类,创建run方法
- * 2,在run方法内,输出线程的名字,让线程停止30ms
- * 3,在主方法内创建3个线程,开启线程
- * @author Administrator
- *
- */
- class ThreadDemo extends Thread {
- public ThreadDemo(String str) {
- super();
- }
- public void run() {
- try {
- while(true){
- System.out.println(this.getName());
- sleep(30);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- return;
- }
- }
- }
- public class TestThread {
- public static void main( String args[] ) {
- ThreadDemo thread1=new ThreadDemo("T1");
- ThreadDemo thread2=new ThreadDemo("T2");
- ThreadDemo thread3=new ThreadDemo("T3");
- thread1.start();
- thread2.start();
- thread3.start();
- }
- }
复制代码
|
|