已经研究如下
先写一个类继承线程- package com.lzf.threaddemo;
- public class MyThread extends Thread{
- public MyThread(String str){
- super(str);
- }
- public void run(){
- for(int i=0;i<20;i++){
- System.out.println(" "+this.getName());
- try {
- sleep(300);
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println(e.getMessage());
- return;
- }
- }
- System.out.println(" /end");
- }
- }
复制代码
然后在主类中测试
- package com.lzf.threaddemo;
- public class MyMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- MyThread myThread1=new MyThread("t1");
- MyThread myThread2=new MyThread("t2");
- MyThread myThread3=new MyThread("t3");
- myThread1.start();
- myThread2.start();
- myThread3.start();
- }
- }
复制代码 |