zoukankan      html  css  js  c++  java
  • Java多线程详解(四)------复习

    1、线程状态

    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;(终结)
    }
     
     

     2、wait/sleep的区别

    wait/sleep
    功能都是当前线程暂停,有什么区别?
    wait放开手去睡,放开手里的锁
    sleep握紧手去睡,醒了手里还有锁

    3、什么是并发?什么是并行?

    并发:同一时刻多个线程在访问同一个资源,多个线程对一个点
    例子:小米9今天上午10点,限量抢购
    春运抢票
    电商秒杀...
    并行:多项工作一起执行,之后再汇总
    例子:泡方便面,电水壶烧水,一边撕调料倒入桶中

     4、什么是锁

     锁实现提供了比使用同步方法和语句可以获得的更广泛的锁操作。它们允许更灵活的结构,可能具有非常不同的属性,并且可能支持多个关联的条件对象。

     1 class X {
     2    private final ReentrantLock lock = new ReentrantLock();
     3    // ...
     4  
     5    public void m() {
     6      lock.lock();  // block until condition holds
     7      try {
     8        // ... method body
     9      } finally {
    10        lock.unlock()
    11      }
    12    }
    13  }
    14  
    15  
    16 synchronized与Lock的区别 
    17  
    18 两者区别:
    19 1.首先synchronized是java内置关键字,在jvm层面,Lock是个java类;
    20 2.synchronized无法判断是否获取锁的状态,Lock可以判断是否获取到锁;
    21 3.synchronized会自动释放锁(a 线程执行完同步代码会释放锁 ;b 线程执行过程中发生异常会释放锁),Lock需在finally中手工释放锁(unlock()方法释放锁),否则容易造成线程死锁;
    22 4.用synchronized关键字的两个线程1和线程2,如果当前线程1获得锁,线程2线程等待。如果线程1阻塞,线程2则会一直等待下去,而Lock锁就不一定会等待下去,如果尝试获取不到锁,线程可以不用一直等待就结束了;
    23 5.synchronized的锁可重入、不可中断、非公平,而Lock锁可重入、可判断、可公平(两者皆可)
    24 6.Lock锁适合大量同步的代码的同步问题,synchronized锁适合代码少量的同步问题。

    5、创建线程的方式

      1)继承Thread类

     1 package com.study.thread;
     2 
     3 public class ThreadTest {
     4     public static void main(String[] args) {
     5         for(int i=0;i<10;i++){
     6             //继承Thread类,覆写run()方法,创建多线程
     7             ThreadDemo t = new ThreadDemo();
     8             t.start();
     9         }
    10 
    11     }
    12 }
    13 class ThreadDemo extends Thread{
    14     @Override
    15     public void run(){
    16         System.out.println("hello world");
    17     }
    18 }

      2)实现Runnable接口

     1 package com.study.thread;
     2 
     3 public class ThreadTest {
     4     public static void main(String[] args) {
     5         for (int i = 0; i < 10; i++) {
     6             //继承Thread类,覆写run()方法,创建多线程
     7 //            ThreadDemo t = new ThreadDemo();
     8 //            t.start();
     9             Thread t = new Thread(new RunnableDemo());
    10             t.start();
    11         }
    12 
    13     }
    14 }
    15 
    16 class ThreadDemo extends Thread {
    17     @Override
    18     public void run() {
    19         System.out.println("hello world");
    20     }
    21 }
    22 
    23 class RunnableDemo implements Runnable {
    24 
    25     @Override
    26     public void run() {
    27         System.out.println("hello Runnable");
    28     }
    29 }

      3)用匿名内部类方式

     1 public class ThreadTest {
     2     public static void main(String[] args) {
     3         for (int i = 0; i < 10; i++) {
     4             //继承Thread类,覆写run()方法,创建多线程
     5 //            ThreadDemo t = new ThreadDemo();
     6 //            t.start();
     7 //            Thread t = new Thread(new RunnableDemo());
     8 //            t.start();
     9             new Thread(new Runnable() {
    10                 @Override
    11                 public void run() {
    12                     System.out.println("hello 匿名内部类");
    13                 }
    14             }).start();
    15         }
    16 
    17     }
    18 }

    总结:上诉3种方式中,1和2都要new对象出来(浪费内存),3是直接new接口,推荐使用第3种方式。

  • 相关阅读:
    监控里的主码流和子码流是什么意思
    监控硬盘容量计算
    一个能让你了解所有函数调用顺序的Android库
    电工选线
    oracle linux dtrace
    list all of the Oracle 12c hidden undocumented parameters
    Oracle Extended Tracing
    window 驱动开发
    win7 x64 dtrace
    How to Use Dtrace Tracing Ruby Executing
  • 原文地址:https://www.cnblogs.com/zsy-code/p/13662426.html
Copyright © 2011-2022 走看看