首页 > 编程语言 > 详细

JAVA多线程

时间:2014-10-19 11:24:07      阅读:213      评论:0      收藏:0      [点我收藏+]

1.定义一个Thread的子类

2.重写run方法

3.在需要的定法创建子类对象

4.调用子类对象的start方法(不要调用run方法,调用run就变成单线程了)

 1 public class Main {
 2     public static void main(String[] args) {
 3         SubThread st = new SubThread();
 4         st.start(); // 启动子线程,调用相应的run方法
 5         
 6         for (int i = 1; i < 100; i++) {
 7             System.out.println(Thread.currentThread().getName() + ":" + i);
 8         }
 9     }
10     
11     
12 }
13 
14 // 1.继承thread类
15 // 2.重写run
16 class SubThread extends Thread {
17     public void run() {
18         for (int i = 1; i<= 100; i++) {
19             System.out.println(i);
20         }
21     }
22 }

 

JAVA多线程

原文:http://www.cnblogs.com/baisu/p/4034396.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!