多线程基础篇--线程状态

1.多线程之线程状态

先看一下我网上找到的图:


Paste_Image.png
  • new
    线程被创建出来,通过new Thread();就是创建了一个线程,这个时候创建的线程并没有执行。
  • runnable
    表示可以执行的,也就是调用了start()方法,调用了start()并不是说线程就运行了,而是需要等操作系统的调度,也就是时间片的轮转。
  • running
    这个时候表示线程已经在执行了,也就是获得了时间片,在运行的过程中可能有一些情况会让出CPU,线程会进入另外一种状态。
  1. 当前线程高风亮节让出CPU,也就是调用yeild()方法,是当前线程回到runnable状态。
    2)当前线程调用了sleep(),线程进入了wait状态,也就是图上所说的blocked();如果线程调用join()也是类似情况
    3)当前线程遇到同步代码块,或者同步方法(synchronized)线程会进入blocked状态;如果线程被wait()也也进入blocked状态。
  • blocked
    这个时候表示线程被阻塞了,阻塞的情况有很多,可能sleep(),wait(),synchronized等等。
  • dead
    当前线程执行完了,或者异常退出了。

2.多线程之创建线程

多线程有两种创建方式:

  • implement runnable
//通过实现的方式
    static class MyThread1 implements Runnable {
        public void run() {
            System.out.println("this is implements two method....");
        }
    }
  • extends Thread
  //通过继承的方式
    static class MyThread extends Thread {
        public void run() {
            System.out.println("this is extends thread one method.....");
        }
    }

Thread 和Runnable区别:

  • runnable 比thread灵活,因为Java是单继承的,但是Java可以实现多个接口。
  • runnable可以实现资源共享
    例如:
package com.yuxi;

/**
 * Created by yuxi on 17/1/26.
 */
public class MyThread {
    public static void main(String[] args) {
        MyThreadOne myThreadOne1 = new MyThreadOne();
        myThreadOne1.start();

        MyThreadOne myThreadOne2 = new MyThreadOne();
        myThreadOne2.start();

        MyThreadOne myThreadOne3 = new MyThreadOne();
        myThreadOne3.start();
        MyThreadTwo myThreadTwo = new MyThreadTwo();
        Thread thread1 = new Thread(myThreadTwo);
        Thread thread2 = new Thread(myThreadTwo);
        Thread thread3 = new Thread(myThreadTwo);
        thread2.start();
        thread1.start();
        thread3.start();
    }

    static class MyThreadOne extends Thread {
        private int ticket=10;
        public void run() {
            for (int i = 0; i < 20; i++) {
                if (ticket>0) {
                    System.out.println("当前" + Thread.currentThread().getName() + "买了" + (ticket--) + "票");
                }
            }
        }
    }


    static class MyThreadTwo implements Runnable {
        private int ticket=10;
        public void run() {
            for (int i = 0; i < 20; i++) {
                if (ticket>0) {
                    System.out.println("当前" + Thread.currentThread().getName() + "买了" + (ticket--) + "票");
                }
            }
        }
    }


}

如果是extends结果是:

当前Thread-0买了10票
当前Thread-0买了9票
当前Thread-0买了8票
当前Thread-0买了7票
当前Thread-0买了6票
当前Thread-0买了5票
当前Thread-0买了4票
当前Thread-0买了3票
当前Thread-0买了2票
当前Thread-0买了1票
当前Thread-1买了10票
当前Thread-1买了9票
当前Thread-1买了8票
当前Thread-1买了7票
当前Thread-1买了6票
当前Thread-1买了5票
当前Thread-1买了4票
当前Thread-1买了3票
当前Thread-1买了2票
当前Thread-1买了1票
当前Thread-2买了10票
当前Thread-2买了9票
当前Thread-2买了8票
当前Thread-2买了7票
当前Thread-2买了6票
当前Thread-2买了5票
当前Thread-2买了4票
当前Thread-2买了3票
当前Thread-2买了2票
当前Thread-2买了1票

如果是implement结果是:

当前Thread-1买了10票
当前Thread-0买了9票
当前Thread-1买了8票
当前Thread-0买了7票
当前Thread-0买了5票
当前Thread-1买了6票
当前Thread-1买了3票
当前Thread-1买了2票
当前Thread-0买了4票
当前Thread-2买了1票

原因是因为:
MyThreadTwo myThreadTwo = new MyThreadTwo();
这个对于启动的三个线程是共享的,他的成员变量 private int ticket=10;是公共资源存在竞争。
MyThreadOne myThreadOne1 = new MyThreadOne();
MyThreadOne myThreadOne2 = new MyThreadOne();
MyThreadOne myThreadOne3 = new MyThreadOne();
分别启动了三个,这三个之间是不存在竞争的。

参考文章:
http://www.cnblogs.com/skywang12345/p/3479063.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文主要讲了java中多线程的使用方法、线程同步、线程数据传递、线程状态及相应的一些线程函数用法、概述等。 首先讲...
    李欣阳阅读 7,245评论 1 15
  • Java多线程学习 [-] 一扩展javalangThread类 二实现javalangRunnable接口 三T...
    影驰阅读 8,150评论 1 18
  • 写在前面的话: 这篇博客是我从这里“转载”的,为什么转载两个字加“”呢?因为这绝不是简单的复制粘贴,我花了五六个小...
    SmartSean阅读 10,279评论 12 45
  • 该文章转自:http://blog.csdn.net/evankaka/article/details/44153...
    加来依蓝阅读 12,092评论 3 87
  • 从去年开始就一直就休闲在家。时间过得飞快,转眼一年多过去了,从最初的郁郁寡欢,烦闷,到如今习惯并接受而满心欢喜。 ...
    闪亮的写作者阅读 3,715评论 0 0

友情链接更多精彩内容