Java多线程之Thread

线程的定义

  • 是操作系统进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。 一个线程指的是进程中一个单一顺序的控制流,一个进程中可以并行多个线程,每条线程并行执行不同的任务。
  • 每个线程都有一个优先级,默认是5(1~10,值越大,优先级越高)
  • 创建线程的两种方式:继承Thread类、实现Runnable接口
  • 每个线程都有一个用于标识的名称,一个名称并不一定对应一个线程
    上下文切换:
    CPU在同一时刻只能运行一个线程,当运行一个线程的时候去运行另外一个线程,这个过程就是上下文切换。(其实就是保存和恢复CPU状态的过程)

Thread类的构造方法

public Thread (Runnable runnable)
public Thread (ThreadGroup group , Runnable runnale)
public Thread (String name)
public Thread (ThreadGroup group , String name)
public Thread (Runnable runnable, String name)
public Thread (ThreadGroup group, Runnable runnable, String name)
public Thread (ThreadGroup group, Runnable runnable, String name, long stackSize)

线程的状态

public enum State {
//线程尚未启动
NEW,

//可运行状态
RUNNABLE,

/**
* 阻塞状态
* 处于阻塞状态的线程正在等待监视器锁进入同步块/方法,或在调用后重新输入同步块/方法
*/
BLOCKED,

/**
* 等待状态,调用以下方法可转至该状态
* wait()
* join()
* LockSupport.park()
*/
WAITING,

/**
* 具有指定等待时间的等待线程的线程状态
* sleep()
* wait()
* join()
* LockSupport.parkNanos()
* LockSupport.parkUntil()
*/
TIMED_WAITING,

/**
* 终止状态,已经执行完毕
*/
TERMINATED;
}

相关方法

  • init()
/**
  * Initializes a Thread.
  *
  * @param g the Thread group
  * @param target the object whose run() method gets called
  * @param name the name of the new Thread
  * @param stackSize the desired stack size for the new thread, or
  *        zero to indicate that this parameter is to be ignored.
  * @param acc the AccessControlContext to inherit, or
  *            AccessController.getContext() if null
  * @param inheritThreadLocals if {@code true}, inherit initial values for
  *            inheritable thread-locals from the constructing thread
  */
 private void init(ThreadGroup g, Runnable target, String name,
                   long stackSize, AccessControlContext acc,
                   boolean inheritThreadLocals) {
     if (name == null) {
         throw new NullPointerException("name cannot be null");
     }

     this.name = name;

     Thread parent = currentThread();
     SecurityManager security = System.getSecurityManager();
     if (g == null) {
         /* Determine if it's an applet or not */

         /* If there is a security manager, ask the security manager
            what to do. */
         if (security != null) {
             g = security.getThreadGroup();
         }

         /* If the security doesn't have a strong opinion of the matter
            use the parent thread group. */
         if (g == null) {
             g = parent.getThreadGroup();
         }
     }

     /* checkAccess regardless of whether or not threadgroup is
        explicitly passed in. */
     g.checkAccess();

     /*
      * Do we have the required permissions?
      */
     if (security != null) {
         if (isCCLOverridden(getClass())) {
             security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
         }
     }

     g.addUnstarted();

     this.group = g;
     this.daemon = parent.isDaemon();
     this.priority = parent.getPriority();
     if (security == null || isCCLOverridden(parent.getClass()))
         this.contextClassLoader = parent.getContextClassLoader();
     else
         this.contextClassLoader = parent.contextClassLoader;
     this.inheritedAccessControlContext =
             acc != null ? acc : AccessController.getContext();
     this.target = target;
     setPriority(priority);
     if (inheritThreadLocals && parent.inheritableThreadLocals != null)
         this.inheritableThreadLocals =
             ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
     /* Stash the specified stack size in case the VM cares */
     this.stackSize = stackSize;

     /* Set thread ID */
     tid = nextThreadID();
 }
  • start()
/**
  * Causes this thread to begin execution; the Java Virtual Machine
  * calls the <code>run</code> method of this thread.
  * <p>
  * The result is that two threads are running concurrently: the
  * current thread (which returns from the call to the
  * <code>start</code> method) and the other thread (which executes its
  * <code>run</code> method).
  * <p>
  * 多次启动一个线程是不合法的
  * It is never legal to start a thread more than once.
  * 一旦线程完成,它就不能重新启动执行
  * In particular, a thread may not be restarted once it has completed
  * execution.
  *
  * @exception  IllegalThreadStateException  if the thread was already
  *               started.
  * @see        #run()
  * @see        #stop()
  */
 public synchronized void start() {
     /**
      * This method is not invoked for the main method thread or "system"
      * group threads created/set up by the VM. Any new functionality added
      * to this method in the future may have to also be added to the VM.
      * O 对应 NEW 状态
      * A zero status value corresponds to state "NEW".
      */
     if (threadStatus != 0)
         throw new IllegalThreadStateException();

     /* Notify the group that this thread is about to be started
      * so that it can be added to the group's list of threads
      * and the group's unstarted count can be decremented. */
     group.add(this);

     boolean started = false;
     try {
         start0();
         started = true;
     } finally {
         try {
             if (!started) {
                 group.threadStartFailed(this);
             }
         } catch (Throwable ignore) {
             /* do nothing. If start0 threw a Throwable then
               it will be passed up the call stack */
         }
     }
 }

 private native void start0();
  • yield() 使当前线程让出CPU资源给其他线程,进入就绪状态,重新获取CPU时间片
/**
  * A hint to the scheduler that the current thread is willing to yield
  * its current use of a processor. The scheduler is free to ignore this
  * hint.
  *
  * <p> Yield is a heuristic attempt to improve relative progression
  * between threads that would otherwise over-utilise a CPU. Its use
  * should be combined with detailed profiling and benchmarking to
  * ensure that it actually has the desired effect.
  *
  * <p> It is rarely appropriate to use this method. It may be useful
  * for debugging or testing purposes, where it may help to reproduce
  * bugs due to race conditions. It may also be useful when designing
  * concurrency control constructs such as the ones in the
  * {@link java.util.concurrent.locks} package.
  */
 public static native void yield();
  • join() 当一个线程必须等待另外一个线程执行完毕才能执行时
/**
  * Waits at most {@code millis} milliseconds for this thread to
  * die. A timeout of {@code 0} means to wait forever.
  *
  * <p> This implementation uses a loop of {@code this.wait} calls
  * conditioned on {@code this.isAlive}. As a thread terminates the
  * {@code this.notifyAll} method is invoked. It is recommended that
  * applications not use {@code wait}, {@code notify}, or
  * {@code notifyAll} on {@code Thread} instances.
  *
  * @param  millis
  *         the time to wait in milliseconds
  *
  * @throws  IllegalArgumentException
  *          if the value of {@code millis} is negative
  *
  * @throws  InterruptedException
  *          if any thread has interrupted the current thread. The
  *          <i>interrupted status</i> of the current thread is
  *          cleared when this exception is thrown.
  */
 public final synchronized void join(long millis)
 throws InterruptedException {
     long base = System.currentTimeMillis();
     long now = 0;

     if (millis < 0) {
         throw new IllegalArgumentException("timeout value is negative");
     }

     if (millis == 0) {
         while (isAlive()) {
             wait(0);
         }
     } else {
         while (isAlive()) {
             long delay = millis - now;
             if (delay <= 0) {
                 break;
             }
             wait(delay);
             now = System.currentTimeMillis() - base;
         }
     }
 }
  • sleep() 调用sleep方法之后线程进入阻塞状态,不会释放对象锁
/**
  * Causes the currently executing thread to sleep (temporarily cease
  * execution) for the specified number of milliseconds, subject to
  * the precision and accuracy of system timers and schedulers. The thread
  * does not lose ownership of any monitors.
  *
  * @param  millis
  *         the length of time to sleep in milliseconds
  *
  * @throws  IllegalArgumentException
  *          if the value of {@code millis} is negative
  *
  * @throws  InterruptedException
  *          if any thread has interrupted the current thread. The
  *          <i>interrupted status</i> of the current thread is
  *          cleared when this exception is thrown.
  */
 public static native void sleep(long millis) throws InterruptedException;
  • interrupt()
/**
  * Interrupts this thread.
  *
  * <p> Unless the current thread is interrupting itself, which is
  * always permitted, the {@link #checkAccess() checkAccess} method
  * of this thread is invoked, which may cause a {@link
  * SecurityException} to be thrown.
  *
  * <p> If this thread is blocked in an invocation of the {@link
  * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
  * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
  * class, or of the {@link #join()}, {@link #join(long)}, {@link
  * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
  * methods of this class, then its interrupt status will be cleared and it
  * will receive an {@link InterruptedException}.
  *
  * <p> If this thread is blocked in an I/O operation upon an {@link
  * java.nio.channels.InterruptibleChannel InterruptibleChannel}
  * then the channel will be closed, the thread's interrupt
  * status will be set, and the thread will receive a {@link
  * java.nio.channels.ClosedByInterruptException}.
  *
  * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
  * then the thread's interrupt status will be set and it will return
  * immediately from the selection operation, possibly with a non-zero
  * value, just as if the selector's {@link
  * java.nio.channels.Selector#wakeup wakeup} method were invoked.
  *
  * <p> If none of the previous conditions hold then this thread's interrupt
  * status will be set. </p>
  *
  * <p> Interrupting a thread that is not alive need not have any effect.
  *
  * @throws  SecurityException
  *          if the current thread cannot modify this thread
  *
  * @revised 6.0
  * @spec JSR-51
  */
 public void interrupt() {
     if (this != Thread.currentThread())
         checkAccess();

     synchronized (blockerLock) {
         Interruptible b = blocker;
         if (b != null) {
             interrupt0();           // Just to set the interrupt flag
             b.interrupt(this);
             return;
         }
     }
     interrupt0();
 }
  • destroy() 已过时
  • stop() 已过时
  • suspend() 已过时
  • resume() 已过时

线程状态转换

附:网上找的贴图一张
线程状态转换

Java中用到的线程调度算法:
抢占式,一个线程用完CPU之后,操作系统会根据线程优先级、线程饥饿情况等数据算出一个总优先级并分配下一个时间片给某个线程执行。
Thread.sleep(0)的作用 ?
由于Java采用的是抢占式调度算法,因此可能会出现某条线程长春获取到CPU控制权的情况。为了使某些优先级比较低的线程也能获取到CPU控制权,使用Thread.sleep(0)可以手动触发一次操作系统分配时间片的操作,也是平衡CPU控制权的一种操作。
#

守护线程

  • thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。
  • 在Daemon线程中产生的新线程也是Daemon的
  • 不要认为所有的应用都可以分配给守护线程来进行服务,比如读写操作或者计算逻辑,java的线程池会将守护线程转换为用户线程,所以如果要使用后台线程就不能用java的线程池
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值