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

Java多线程例子讲解

时间:2014-08-19 09:25:03      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:blog   http   java   strong   ar   2014   art   log   

一:知识点声明:

1.区别进程和线程:进程是静态概念,它的执行依赖线程进行。

2.进程的状态:就绪(等待cpu执行),运行,中止,阻塞(等待所需资源,进入阻塞态)

3.Java程序的main函数即是一个线程,被称做主线程。此时如果新建线程,则和主线程一起并行运行。

4.Java中的构造方法、main函数谁先执行?

main函数先执行,因为main是静态方法,程序一开始就执行;而构造方法只有在类实例化时才去调用。


二:实例程序

public class GetCurrentThread implements Runnable {
Thread th;

public GetCurrentThread(String threadName) {
th = new Thread(this,threadName); //<----DOUBT
System.out.println("get threadname "+th.getName());
th.start();
}

public void run() {
System.out.println(th.getName()+" is starting.....");
System.out.println("Current thread name : " + Thread.currentThread().getName());
}

public static void main(String args[]) {
System.out.println("Current thread name : " + Thread.currentThread().getName());
new GetCurrentThread("1st Thread");
//new GetCurrentThread("2nd Thread");
}
}


三:执行结果

bubuko.com,布布扣

四:程序分析过程(直接用笔记bubuko.com,布布扣

bubuko.com,布布扣


Java多线程例子讲解,布布扣,bubuko.com

Java多线程例子讲解

标签:blog   http   java   strong   ar   2014   art   log   

原文地址:http://blog.csdn.net/guomutian911/article/details/38676871

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