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

java 多线程学习(一)

时间:2014-07-16 18:33:09      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   art   for   

 1 public class ThreadA extends Thread {
 2     private static int threadID = 0;
 3 
 4     public ThreadA() {
 5         super("ThreadID:" + (++threadID));
 6     }
 7 
 8     public void run() {
 9         try {
10             System.out.println(getName() + " 线程运行开始!");
11             for (int i = 0; i < 10; i++) {
12                 System.out.println(this.getName() + " is " + i);
13 
14                 Thread.sleep(1);//这里要sleep,不然看不出多线程的效果
15             }
16         } catch (Exception e) {
17             e.printStackTrace();
18         }
19     }
20 
21 }
public class MyTest {

    public static void main(String[] args){
        ThreadA t1=new ThreadA();
        ThreadA t2=new ThreadA();
        t1.start();
        t2.start();
    }
}

输入结果:

ThreadID:1 线程运行开始!
ThreadID:1 is 0
ThreadID:2 线程运行开始!
ThreadID:2 is 0
ThreadID:1 is 1
ThreadID:2 is 1
ThreadID:1 is 2
ThreadID:2 is 2
ThreadID:1 is 3
ThreadID:2 is 3
ThreadID:1 is 4
ThreadID:2 is 4
ThreadID:1 is 5
ThreadID:2 is 5
ThreadID:1 is 6
ThreadID:2 is 6
ThreadID:1 is 7
ThreadID:2 is 7
ThreadID:1 is 8
ThreadID:2 is 8
ThreadID:1 is 9
ThreadID:2 is 9

java 多线程学习(一),布布扣,bubuko.com

java 多线程学习(一)

标签:style   blog   java   color   art   for   

原文地址:http://www.cnblogs.com/csu_xajy/p/3845250.html

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