java 线程的使用

java 线程的使用

//线程的使用
//需要记三个单词
//1、Thread        线程的类名
//2、 Runnable    线程的接口
//3、 start        执行线程

//使用继承线程类的方式实现线程
class xc1 extends Thread{
    public void run(){
        for(int i=0;i<20;i++){
            System.out.println("次(继承类)线程的内容"+i);
        }
    }
}

//使用实现接口的方式实现线程(推荐使用)
class xc2 implements Runnable{
    public void run(){
        for(int i=0;i<20;i++){
            System.out.println("次(接口实现)线程的内容"+i);
        }
    }
}




public class Index{
    public static void main(String[] args){
        
        //执行继承类的线程
        xc1    xc1    =    new xc1();
            xc1.start();
            
        //执行接口的线程
        xc2        xc2            =    new    xc2();
        Thread    xc2_Thread    =    new    Thread(xc2);
                xc2_Thread.start();
        
        
        for(int i=20;i<40;i++){
            System.out.println("主线程的内容"+i);
        }
        
    }
}

.

原文地址:https://www.cnblogs.com/phpyangbo/p/5964289.html