P364 实战练习(多线程)

尝试定义一个继承Thread类的类,并覆盖run( )方法,在run( )方法中每隔1000毫秒打印一句话。

编写代码如下:

编写PractiseThread类:

 1 package org.hanqi.practise;
 2 
 3 public class PractiseThread extends Thread{
 4     
 5     public void run()
 6     {
 7         for(int i=1;i<5;i++)
 8         {
 9             int j;
10             switch(j=i)
11             {
12             case 1:
13                 System.out.println("三行情书:");
14                 break;
15             case 2:
16                 System.out.println("鸡蛋灌饼");
17                 break;
18             case 3:
19                 System.out.println("两个");
20                 break;
21             case 4:
22                 System.out.println("她的加里脊");
23                 break;                
24             }
25             try {
26                 Thread.sleep(1000);
27             } catch (InterruptedException e) {
28                 // TODO 自动生成的 catch 块
29                 e.printStackTrace();
30             }
31         }
32     }
33 }

编写TestThread测试类:

 1 package org.hanqi.practise;
 2 
 3 public class TestThread {
 4 
 5     public static void main(String[] args) {
 6         
 7         PractiseThread pt = new PractiseThread();
 8         pt.start();
 9     }
10 }

运行结果为:

,每隔1000毫秒输出一句话。

原文地址:https://www.cnblogs.com/hanazawalove/p/5280743.html