码迷,mamicode.com
首页 > 编程语言 > 详细

课堂所讲整理:多线程

时间:2016-03-15 20:38:43      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

创建Threadtest类:

 1 package org.hanqi.thread;
 2 
 3 //支持多线程
 4 //1.继承Thread
 5 //2.覆盖run方法
 6 public class TestThread extends Thread {    //extends
 7     
 8     //重写
 9     public void run()
10     {
11         for(int i=0;i<10;i++)
12         {
13             System.out.print(i+" ");
14             try {
15                 //线程的休眠方法(毫秒)
16                 Thread.sleep(1000);
17             } catch (InterruptedException e) {
18                 // TODO 自动生成的 catch 块
19                 e.printStackTrace();
20             }
21         }
22     }
23 }

创建TestThread1类:

 1 package org.hanqi.thread;
 2 
 3 public class TestThread1 implements Runnable{    //implements
 4 
 5     @Override
 6     public void run() {
 7         for(int i=0;i<10;i++)
 8         {
 9             System.out.println(i);
10             try {
11                 //线程的休眠方法(毫秒)
12                 Thread.sleep(1000);
13             } catch (InterruptedException e) {
14                 // TODO 自动生成的 catch 块
15                 e.printStackTrace();
16             }
17         }        
18     }
19 }

创建Test1类运行:

 1 package org.hanqi.thread;
 2 
 3 public class Test1 {
 4 
 5     public static void main(String[] args) {
 6         
 7         for(int i=0;i<10;i++)
 8         {
 9             System.out.print(i+" ");
10             try {
11                 //线程的休眠方法(毫秒)
12                 Thread.sleep(1000);
13             } catch (InterruptedException e) {
14                 // TODO 自动生成的 catch 块
15                 e.printStackTrace();
16             }
17         }
18         System.out.println();
19         TestThread tt = new TestThread();
20         //启动多线程
21         tt.start();
22         TestThread tt1 = new TestThread();
23         //启动多线程
24         tt1.start();
25         //启动实现接口方式的多线程
26         Thread tt2 = new Thread(new TestThread1());
27         tt2.start();
28     }
29 }

运行结果为:

技术分享

附相关思维导图:

技术分享

课堂所讲整理:多线程

标签:

原文地址:http://www.cnblogs.com/hanazawalove/p/5280727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!