实践Java线程

main主线程和自定义线程(Thread),自定义线程包括new Thread(Runnable target)继承Thread

daemon

当所有非daemon线程运行结束,则jvm会强制结束,不管此时daemon是否还在执行。

main主线程是非daemon,

线程状态

线程状态及转换

  • New(新生)

  • Runnable(可运行):包含操作系统中的就绪运行中

  • Blocked(被阻塞):等待获取锁(waiting for monitor entry

    1
    2
    3
    4
    "RMI TCP Connection(64896)-172.16.52.118" daemon prio=10 tid=0x00000000405a6000 nid=0x68fe waiting for monitor entry [0x00007f2be65a3000]
       java.lang.Thread.State: BLOCKED (on object monitor)
    at com.xyz.goods.service.impl.GoodsServiceImpl.findChanellGoodsCountWithCache(GoodsServiceImpl.java:1734)
    - waiting to lock <0x00000000fe7e3b50> (a java.lang.String)
    
  • Waiting(等待):等待某个条件发生(waiting on condition

    线程池等待任务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    "http-nio-8080-exec-1" #163 daemon prio=5 os_prio=0 tid=0x00007f13dc7aa800 nid=0x6b96 waiting on condition [0x00007f13d37f8000]
       java.lang.Thread.State: WAITING (parking)
    	at sun.misc.Unsafe.park(Native Method)
    	- parking to wait for  <0x00000000ead46238> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
    	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
    	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
    	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	at java.lang.Thread.run(Thread.java:748)
    

    线程池等待任务(多余minSpareThreads的线程)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    "http-nio-8080-exec-6" #107 daemon prio=5 os_prio=0 tid=0x00007fec3802d000 nid=0x62e0 waiting on condition [0x00007febcd503000]
       java.lang.Thread.State: TIMED_WAITING (parking)
    	at sun.misc.Unsafe.park(Native Method)
    	- parking to wait for  <0x00000000869935f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
    	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
    	at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
    	at org.apache.tomcat.util.threads.TaskQueue.poll(TaskQueue.java:85)
    	at org.apache.tomcat.util.threads.TaskQueue.poll(TaskQueue.java:31)
    	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1073)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	at java.lang.Thread.run(Thread.java:748)
    

    sleep

    1
    2
    3
    4
    5
    "Dubbo-Holder" #20 prio=5 os_prio=0 tid=0x00007fccc098d000 nid=0x6452 waiting on condition [0x00007fcc87efd000]
     java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at io.dubbo.springboot.DubboHolderListener$1.run(DubboHolderListener.java:28)
    at java.lang.Thread.run(Thread.java:748)
    

    等待队列数据

    1
    2
    3
    4
    "RMI TCP Connection(idle)" daemon prio=10 tid=0x00007fd50834e800 nid=0x56b2 waiting on condition [0x00007fd4f1a59000]
       java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000000acd84de8> (a java.util.concurrent.SynchronousQueue$TransferStack)
    

    同步块中调用wait(in Object.wait()

    1
    2
    3
    4
    "RMI RenewClean-[172.16.50.182:4888]" daemon prio=10 tid=0x0000000040d2c800 nid=0x97e in Object.wait() [0x00007f9ccafd0000]
       java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000799b032d8> (a java.lang.ref.ReferenceQueue$Lock)
    
  • Timed waiting(计时等待):跟Waiting类似,只是计时等待

  • Terminated(被终止)

核心类

Runnable

Callable

Future

RunnableFuture

FutureTask

秒懂Java多线程

秒懂Java并发之volatile关键字引发的思考