java 线程详解及线程与进程的区别 1.进程与线程 每个进程都独享一块内存空间,一个应用程序可以同时启动多个进程。比如IE浏览器,打开一个Ie浏览器就相当于启动了一个进程。 线程指进程中的一个执行流程,一个进程可以包含多个线程。 每个进程都需要操作系统为其分配独立的内存空间,而同一个进程中的多个线程共享这块空间,即共享内存等资源。 每次调用java.exe的时候,操作系统都会启动一个Java虚拟机进程,当启动Java虚拟机进程时候,Java虚拟机都会创建一个主线程,该线程会从程序入口main方法开始执行。 Java虚拟机每启动一个线程,就给会给该线程分配一个线程方法栈,用来存放相关信息(比如局部变量等),线程就在这个栈上运行。所以Java对象中的局部变量都是线程安全的,但实例变量及类变量由于不是保存在栈中,所以不是线程安全的。 进程有三种状态:就绪、执行、阻塞。  2.线程创建方式 Runnable方式:(此种方式灵活,推荐使用) - public class Thread02 implements Runnable {
-
- public static void main(String[] args) {
- Runnable r = new <strong>Thread02</strong>();
- Thread t1 = new Thread(<strong>r</strong>, "t1");
- /**
- * Thread源码
- * public Thread(Runnable target, String name) {
- init(null, target, name, 0);
- }
- */
- Thread t2 = new Thread(r, "t2");
- t1.start(); // 启动线程t1,处于就绪状态,等待cpu
- t2.start(); // 启动线程t2,处于就绪状态,等待cpu
- t1.run(); // 主线程main调用对象t1的run方法
- }
-
- public void run() {
- System.out.println("thread's name is "
- + Thread.currentThread().getName());
- }
-
- }
复制代码 运行结果可能是: - thread's name is t1
- thread's name is main
- thread's name is t2
复制代码 Thead方式 - public class Thread03 extends Thread {
-
- public static void main(String[] args) {
- Thread03 t1 = new <strong>Thread03</strong>(); //不注意的情况下写成了Thread t1=new Thread() 注:Thread03此时就是一个线程了
- t1.start();
- }
-
- public void run() {
- System.out.println("thread's name is "
- + Thread.currentThread().getName());
- }
- }
复制代码 运行结果:thread's name is Thread-0 注意:每次程序运行时除了自定义的线程外还有一个main线程。 综合: - public class Thread01 {
- public static void main(String[] args) {
- Thread thread=new Thread();
- thread.start();//真正起作用 的是run()
- /**而Thread中的run
- * public void run() {
- if (target != null) {
- target.run();
- }
- }
- 所以自己创建的线程要重写run方法,把要执行的内容放到run()中,所以要实现接口或继承进而产生子类
- */
-
- //创建线程的方式1 thread子类方式(继承)
- Thread thread1=new Thread(){
- public void run() {
- while(true){
- try {
- Thread.sleep(500);//休息500毫秒
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- //Thread.currentThread()得到当前线程
- System.out.println("线程1的名字是 "+Thread.currentThread().getName());
- }
- }
- };
- // thread1.start(); //不写 线程无法启动
-
-
- //创建线程的方式2 runnable方式(实现) 推荐使用
- Thread thread2=new Thread(new Runnable(){
- public void run() {
- while(true){
- try {
- Thread.sleep(300);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("thread2'name is "+Thread.currentThread().getName());
-
- }
- }});
- // thread2.start();
-
- //执行的是thread
- new Thread(new Runnable(){
- public void run() {
- System.out.println("runnable "+Thread.currentThread().getName());
- }}){
- public void run() { //子类中的run方法覆盖父类中的run方法,这样就不会执行runnable
- System.out.println("thread "+Thread.currentThread().getName());
- }
- }.start();
- }
- /***
- * 在单个cpu中执行多线程很有可能降低执行效率而不是提高 一个人在不同地方做同一件事情
- */
- }
复制代码 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持! |