多线程二

1 线程的生命周期

  • 在JDK中用Thread.State枚举定义了线程的几种状态。
public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }
View Code
  • 要想实现多线程,必须在主线程中创建新的线程对象。Java语言使用Thread类及其子类的对象来表示线程,在它的一个完整的生命周期中通常要经历如下的五种状态:
    • 新建:当一个Thread类及其子类的对象被声明并创建的时候,新生的线程对象处于新创建状态。
    • 就绪:处于新建状态的线程被start()后,将进入线程队列等待CPU时间片,此时它已经具备了运行的条件,只是没有分配到CPU的资源。
    • 运行:当就绪的线程被调度并获取CPU的资源的时候,就进入运行状态,run()方法中定义了线程的操作和功能。
    • 阻塞:在某种特殊的情况下,被认为挂起或执行输入输出操作的时候,让CPU临时中止自己的执行,进入阻塞状态。
    • 死亡:线程完成它的全部工作或线程被提前强制性中止或出现异常导致结束。  

2 线程的同步

2.1 线程安全问题出现的原因

  • 多线程环境。
  • 多个线程操作共享数据(当多条语句在操作同一线程共享数据的时候,一个线程对多条语句执行了一部分,还没有执行完,另一个线程参与进来执行,导致共享数据的错误)。 

2.2 线程安全问题的解决之道

  • 对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中,其他的线程不可以参与执行(加锁)。 

2.3 Java中解决线程安全问题

  • Java对于多线程的安全问题体用了专业的解决方式:同步机制。
  • 同步代码块:
synchronized(对象){
   //需要被同步的代码
}
  • 同步方法:synchronized还可以放在方法的声明处,表示整个方法为同步方法。
public synchronized void show(){
   //需要被同步的代码
}

猜你喜欢

转载自www.cnblogs.com/xuweiweiwoaini/p/11006427.html
今日推荐