java 线程测试 旅游 小练习

package com.hanqi.xc;

import java.util.Random;

public class lvyou extends Thread {

    @Override
    public void run() {
        
        
        for(int i=0;i<10;i++)
        {
            
            System.out.println("Thread测试"+" "+Thread.currentThread().getName()  );
        
        try {
            
            Random r1 = new Random();
            
            int a = r1.nextInt(1000);
            
            Thread.sleep(a);
            
        } 
        catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        }
        
    }
    
    

}
package com.hanqi.xc;

public class cs {

    public static void main(String[] args) {
        
        
        lvyou l1 = new lvyou();
        
        l1.setName("拉萨");
        l1.start();
        

        lvyou l2 = new lvyou();
        
        l2.setName("大理");
        l2.start();
        
        
        
        
//        Thread th = new Thread (new lvyou(),"青海");
//        th.start();
//        Thread th2 = new Thread (new lvyou(),"西藏");
//        th2.start();

    }

}

package com.hanqi.xc;

import java.util.Random;

public class lvyou2 implements Runnable {

    @Override
    public void run() {
        
        for(int i=0;i<10;i++)
        {
            
            System.out.println("Runnable测试"+" "+Thread.currentThread().getName()  );
        
        try {
            
            Random r1 = new Random();
            
            int a = r1.nextInt(1000);
            
            Thread.sleep(a);
            
        } 
        catch (InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        }
        

    }

}
package com.hanqi.xc;

public class cs {

    public static void main(String[] args) {
        
        
//        lvyou l1 = new lvyou();
//        
//        l1.setName("拉萨");
//        l1.start();
//        
//
//        lvyou l2 = new lvyou();
//        
//        l2.setName("大理");
//        l2.start();
//        
        
        
        
        Thread th = new Thread (new lvyou2(),"青海");
        th.start();
        Thread th2 = new Thread (new lvyou2(),"西藏");
        th2.start();

    }

}

原文地址:https://www.cnblogs.com/zhailiming/p/5558154.html