Java多线程例子讲解
2014-08-19 08:50
316 查看
一:知识点声明:
1.区别进程和线程:进程是静态概念,它的执行依赖线程进行。
2.进程的状态:就绪(等待cpu执行),运行,中止,阻塞(等待所需资源,进入阻塞态)
3.Java程序的main函数即是一个线程,被称做主线程。此时如果新建线程,则和主线程一起并行运行。
4.Java中的构造方法、main函数谁先执行?
main函数先执行,因为main是静态方法,程序一开始就执行;而构造方法只有在类实例化时才去调用。
二:实例程序
三:执行结果
四:程序分析过程(直接用笔记
)
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"); } }
三:执行结果
四:程序分析过程(直接用笔记
)
相关文章推荐
- rpcgen的简单讲解及例子程序
- HttpClient4.1入门教程-利用官方例子讲解httpClient4.1的用法
- Mars老师讲解的装饰者模式代码例子
- android 与C/C++混合编程小例子讲解o
- 面向对象编程设计模式--简单工厂模式讲解(历史上最简单明白的例子)
- 可视化工具solo show-----Prefuse自带例子GraphView讲解
- 简明的例子讲解position:relative、float、overflow:hidden和inline-block
- C#多线程简单例子讲解
- C#多线程简单例子讲解
- MDX的实例讲解(排名前15的小例子)
- 今天来用一个例子来详细讲解下nutch当中到底是如何自定义插件的
- StatusBar的使用_例子讲解
- C#多线程简单例子讲解
- 马老师的WoTou生产消费线程讲解例子
- 用神经网络例子讲解TF运行方式~人工智能入门编程例子讲解
- 面向对象编程设计模式--简单工厂模式讲解(历史上最简单明白的例子)
- Python 列表解析 例子 讲解 学习 list comprehension in python demo
- 如何用简单例子讲解 Q - learning 的具体过程?
- c++异常处理机制示范例子及讲解
- MDX的实例讲解(排名前15的小例子)