初步线程学习1

线程演示:

 1 package thread;
 2 
 3 /**
 4  * 兔子的线程
 5  * @author superdrew
 6  */
 7 public class RabbitThread extends Thread{
 8     public void run() {
 9         setName("兔子线程");
10         while(true){
11             System.out.println("兔子领先了....加油!!!!"+getName()+" "+getPriority());
12         }
13     }
14     
15 }
RabbitThread.java

测试线程:

 1 package thread;
 2 
 3 /**
 4  * 功能:龟兔赛跑
 5  *            使用线程
 6  *     思路:分别创建两个线程  一个是乌龟 另外一个是兔子  ,完成赛跑任务
 7  *  总结:1.如何定义线程
 8  *           继承Thread 重写run方法     ,run是线程体,要完成任务
 9  *      2.如何创建线程
10  *          RabbitThread rt = new RabbitThread()
11  *      3.如何启动线程
12  *           rt.start();//启动线程
13  *           run()       只是一个普通的方法
14  *      4.其它
15  *          之前的main是一个单线程,
16  *          实质是 相应的线程获取了cpu并执行线程体 run();
17  *          cpu时间片到了,切换线程
18  *  
19  * @author superdrew
20  *
21  */
22 public class TestThread {
23     public static void main(String[] args) {
24         RabbitThread rt = new RabbitThread();
25         rt.setName("兔子");
26         //rt.run();//普通的方法
27         rt.start();//启动线程
28         while(true){//乌龟有机会吗?
29             System.out.println("乌龟领先了....."+
30                     Thread.currentThread().getName()+" "+Thread.currentThread().getPriority());
31         }
32 
33     }
34 }
main_Thread
原文地址:https://www.cnblogs.com/superdrew/p/8092917.html