Java多线程学习系列

"1-创建线程"

Posted by Dunno on January 16, 2016

创建线程

创建一个线程

  • 实现Runnable接口
      public TestRunnable implements implements Runnable {
          @Override
          public void run() {
          }
      }
      new Thread(new TestRunnable()).start();
      
  • 继承Thread类
      public class TestThread extends Thread {
          @Override
          public void run() {
          }
      }
      new TestThread().start();
      

启动一个线程

  • 启动一个线程
      new Thread().start();
      

线程的状态

  • NEW 当线程创建之后但是还没有调用start方法时会是这个状态。
  • RUNNABLE 当线程正在JVM中执行时(但实际也有可能正在等待操作系统的其他资源,比如处理器资源),线程会被标记成该状态
  • BOLCKED 线程正在等待一个监视器锁或者进入同步块/方法,或者在调用Object.wait()方法之后重进入同步块/方法
  • WAITING 线程正在等待另一个线程执行完一个特定的动作时会标记成该状态,比如:一个线程被调用了Object.wait()会使它处于WAITING状态直到另一个线程调用了Object.notify()或者Object.notifyAll()唤醒该线程;或者该线程被调用了Object.join()等待另一个线程的终止时会处于WAITING状态。
  • TIMED_WAITING 在特定的时间内等待另一个线程。以下方法会产生该作用:
    • Thread.sleep
    • Object.wait with timeout
    • Thread.join with timeout
    • LockSupport.parkNanos
    • LockSupport.parkUntil
  • TERMINATED 线程的终止状态,当线程完成它的执行时标记成该状态。

start和run的区别

  • start的实现
/**
     * Causes this thread to begin execution; the Java Virtual Machine
     * calls the run method of this thread.
     * 

* The result is that two threads are running concurrently: the * current thread (which returns from the call to the * start method) and the other thread (which executes its * run method). *

* 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. * * 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 */ } } } </pre> * run的实现

	/**
     * If this thread was constructed using a separate
     * Runnable run object, then that
     * Runnable object's run method is called;
     * otherwise, this method does nothing and returns.
     * 

* Subclasses of Thread should override this method. * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */ @Override public void run() { if (target != null) { target.run(); } } </pre> * 方法的区别 * start方法是一个线程开始执行的原因(所以我们应该通过调用start方法来启动一个线程,而不是run) * start方法至多启动同一个线程一次。 * 线程再完成执行后,不会重新启动start方法。 * start方法不会被“主线程”或者是虚拟机创建/设置的“系统态”线程调用(而是创建一个新线程来调用)。 * run就是一个普通的方法,可以被重复调用。