您的位置:首页 > 编程语言 > Java开发

Java多线程例子讲解

2014-08-19 08:50 316 查看
一:知识点声明:

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");
}
}


三:执行结果





四:程序分析过程(直接用笔记






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: