1. 启动netty server 等待接受客户端连接

 package io.netty.example.myTest.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.util.Set; public class ServerNIO {
static Selector selector;
static ServerSocketChannel serverSocketChannel;
public static void main (String[] args) {
try {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress("localhost", 9999));
serverSocketChannel.configureBlocking(false);
SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while ( true ) {
selector.select(1000);
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> iKeys = keys.iterator();
SelectionKey selectionKey1 = null;
while (iKeys.hasNext()) {
selectionKey1 = iKeys.next();
iKeys.remove();
if (selectionKey1.isValid()) {
if (selectionKey1.isAcceptable()) {
Thread.sleep(10000);
System.out.println("accept");
}
}
}
}
}catch (IOException ioe) { } catch (InterruptedException e) {
e.printStackTrace();
}
}
}

jps 查看进程号

jstack查看进程状态,处于RUNNABLE 并在ServerNIO.main(ServerNIO.java:21) 处,调用栈看到 EPollArrayWrapper.epollWait

[root@izm5e8p93wtcdi53pzqhi7z ~]# jstack 29266

2018-07-30 13:06:39
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode): "Attach Listener" #8 daemon prio=9 os_prio=0 tid=0x00007f2e28001000 nid=0x7289 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007f2e580ba000 nid=0x725c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f2e580b7000 nid=0x725b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f2e580b4000 nid=0x725a waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f2e580b2800 nid=0x7259 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f2e5807f800 nid=0x7258 in Object.wait() [0x00007f2e5c4de000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) "Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f2e5807d000 nid=0x7257 in Object.wait() [0x00007f2e5c5df000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "main" #1 prio=5 os_prio=0 tid=0x00007f2e58009000 nid=0x7253 runnable [0x00007f2e61a9a000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000000f6775890> (a sun.nio.ch.Util$3)
- locked <0x00000000f6775808> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000000f6770c10> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at ServerNIO.main(ServerNIO.java:21)
"VM Thread" os_prio=0 tid=0x00007f2e58073000 nid=0x7256 runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f2e5801e800 nid=0x7254 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f2e58020000 nid=0x7255 runnable "VM Periodic Task Thread" os_prio=0 tid=0x00007f2e580be800 nid=0x725d waiting on condition JNI global references: 14

2. 启动一个客户端,仅连接

package io.netty.example.myTest.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set; public class ClientNIO {
static Selector selector;
static SocketChannel socketChannel;
public static void main (String[] args) {
try {
socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("localhost", 9999));
}catch (IOException ioe) { }
}
}

连接后断开,再次运行[root@izm5e8p93wtcdi53pzqhi7z ~]# jstack 29266,可以看到ServerNIO.main(ServerNIO.java:30)  此时已经连接并且在sleep,java.lang.Thread.State: TIMED_WAITING (sleeping)

2018-07-30 13:20:08
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode): "Attach Listener" #8 daemon prio=9 os_prio=0 tid=0x00007f2e28001000 nid=0x7289 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007f2e580ba000 nid=0x725c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f2e580b7000 nid=0x725b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f2e580b4000 nid=0x725a waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f2e580b2800 nid=0x7259 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f2e5807f800 nid=0x7258 in Object.wait() [0x00007f2e5c4de000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) "Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f2e5807d000 nid=0x7257 in Object.wait() [0x00007f2e5c5df000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "main" #1 prio=5 os_prio=0 tid=0x00007f2e58009000 nid=0x7253 waiting on condition [0x00007f2e61a9a000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ServerNIO.main(ServerNIO.java:30)
"VM Thread" os_prio=0 tid=0x00007f2e58073000 nid=0x7256 runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f2e5801e800 nid=0x7254 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f2e58020000 nid=0x7255 runnable "VM Periodic Task Thread" os_prio=0 tid=0x00007f2e580be800 nid=0x725d waiting on condition JNI global references: 14

3. CPU过高(忙于计算,忙于GC - https://www.jianshu.com/p/f93b6013b965)

忙于死循环类测试

 package io.netty.example.myTest;

 import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ThreadTest {
public static void main (String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
int i = 0;
while ( true ) {
i++;
i--;
}
}
});
}
}

top -Hp 29926

printf %x 29938  ==》 74f2, 根据该线程号可以找到nid=0x74f2的信息,进而看到 ThreadTest$1.run(ThreadTest.java:13)

2018-07-30 13:41:00
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode): "Attach Listener" #10 daemon prio=9 os_prio=0 tid=0x00007fcdc4001000 nid=0x7519 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "DestroyJavaVM" #9 prio=5 os_prio=0 tid=0x00007fcdfc009000 nid=0x74e7 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "pool-1-thread-1" #8 prio=5 os_prio=0 tid=0x00007fcdfc0ef800 nid=0x74f2 runnable [0x00007fcde75f4000]
java.lang.Thread.State: RUNNABLE
at ThreadTest$1.run(ThreadTest.java:13)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007fcdfc0c2000 nid=0x74f0 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fcdfc0b7000 nid=0x74ef waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fcdfc0b4000 nid=0x74ee waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fcdfc0b2800 nid=0x74ed runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fcdfc07f800 nid=0x74ec in Object.wait() [0x00007fcde7bfa000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) "Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fcdfc07d000 nid=0x74eb in Object.wait() [0x00007fcde7cfb000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "VM Thread" os_prio=0 tid=0x00007fcdfc073000 nid=0x74ea runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fcdfc01e800 nid=0x74e8 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fcdfc020000 nid=0x74e9 runnable "VM Periodic Task Thread" os_prio=0 tid=0x00007fcdfc0c7000 nid=0x74f1 waiting on condition JNI global references: 5

4. 对于3改成不用线程池再看一下

new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while ( true ) {
i++;
i--;
}
}
}).start();

截取部分内容,看到区别

"Thread-0" #8 prio=5 os_prio=0 tid=0x00007fb8000df800 nid=0x757a runnable [0x00007fb7f02f1000]
java.lang.Thread.State: RUNNABLE
at ThreadTest$
1.run(ThreadTest.java:9)
at java.lang.Thread.run(Thread.java:748)

5. wait的情况

 import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ThreadTest {
public static void main (String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.execute(new Runnable() {
@Override
public void run() {
Object lock = new Object();
synchronized ( lock ) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
"pool-1-thread-1" #8 prio=5 os_prio=0 tid=0x00007f88880ef800 nid=0x75c5 in Object.wait() [0x00007f887539d000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f67a8010> (a java.lang.Object)
at java.lang.Object.wait(Object.java:502)
at ThreadTest$1.run(ThreadTest.java:13)
- locked <0x00000000f67a8010> (a java.lang.Object)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

6. 带时间的wait(10000)

"pool-1-thread-1" #8 prio=5 os_prio=0 tid=0x00007f498c0f7800 nid=0x7607 in Object.wait() [0x00007f4977cfb000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f67a8010> (a java.lang.Object)
at ThreadTest$1.run(ThreadTest.java:13)
- locked <0x00000000f67a8010> (a java.lang.Object)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

7. lock

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class ThreadTest {
public static void main (String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.execute(new Runnable() {
@Override
public void run() {
Lock lock = new ReentrantLock();
lock.lock();
}
});
}
}
"pool-1-thread-1" #8 prio=5 os_prio=0 tid=0x00007fbe9c0ff800 nid=0x763f waiting on condition [0x00007fbe8cbfa000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000f675d9b0> (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 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 java.lang.Thread.run(Thread.java:748)

8.死锁测试

 import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ThreadTest {
public static void main (String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
final Object A = new Object();
final Object B = new Object(); final Runnable command1 = new Runnable() {
@Override
public void run() {
synchronized ( A ) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized ( B ) { }
}
}
};
final Runnable command2 = new Runnable() {
@Override
public void run() {
synchronized ( B ) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized ( A ) { }
}
}
};
executorService.execute(command1);
executorService.execute(command2);
}
}
2018-07-30 14:09:06
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode): "Attach Listener" #11 daemon prio=9 os_prio=0 tid=0x00007f3ef8001000 nid=0x7728 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "DestroyJavaVM" #10 prio=5 os_prio=0 tid=0x00007f3f28009000 nid=0x7703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "pool-1-thread-2" #9 prio=5 os_prio=0 tid=0x00007f3f280e9000 nid=0x770f waiting for monitor entry [0x00007f3f18af9000]
java.lang.Thread.State: BLOCKED (on object monitor)
at ThreadTest$2.run(ThreadTest.java:36)
- waiting to lock <0x00000000f675de30> (a java.lang.Object)
- locked <0x00000000f675de40> (a java.lang.Object)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748) "pool-1-thread-1" #8 prio=5 os_prio=0 tid=0x00007f3f280e7800 nid=0x770e waiting for monitor entry [0x00007f3f18bfa000]
java.lang.Thread.State: BLOCKED (on object monitor)
at ThreadTest$1.run(ThreadTest.java:21)
- waiting to lock <0x00000000f675de40> (a java.lang.Object)
- locked <0x00000000f675de30> (a java.lang.Object)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007f3f280ba000 nid=0x770c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f3f280b7000 nid=0x770b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f3f280b4000 nid=0x770a waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f3f280b2800 nid=0x7709 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f3f2807f800 nid=0x7708 in Object.wait() [0x00007f3f2c28e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000f6708ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) "Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f3f2807d000 nid=0x7707 in Object.wait() [0x00007f3f2c38f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000f6706bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "VM Thread" os_prio=0 tid=0x00007f3f28073000 nid=0x7706 runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f3f2801e800 nid=0x7704 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f3f28020000 nid=0x7705 runnable "VM Periodic Task Thread" os_prio=0 tid=0x00007f3f280bf000 nid=0x770d waiting on condition JNI global references: 5 Found one Java-level deadlock:
=============================
"pool-1-thread-2":
waiting to lock monitor 0x00007f3f040062c8 (object 0x00000000f675de30, a java.lang.Object),
which is held by "pool-1-thread-1"
"pool-1-thread-1":
waiting to lock monitor 0x00007f3f040038d8 (object 0x00000000f675de40, a java.lang.Object),
which is held by "pool-1-thread-2" Java stack information for the threads listed above:
===================================================
"pool-1-thread-2":
at ThreadTest$2.run(ThreadTest.java:36)
- waiting to lock <0x00000000f675de30> (a java.lang.Object)
- locked <0x00000000f675de40> (a java.lang.Object)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"pool-1-thread-1":
at ThreadTest$1.run(ThreadTest.java:21)
- waiting to lock <0x00000000f675de40> (a java.lang.Object)
- locked <0x00000000f675de30> (a java.lang.Object)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748) Found 1 deadlock.

9. 内存溢出

参数简介

-Xmn:整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小。持久代一般固定大小为64m,所以增大年轻代后,将会减小年老代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8。
-Xss128k:设置每个线程的堆栈大小,在相同物理内存下,减小这个值能生成更多的线程。但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000左右
-XX:NewRatio=4:设置年轻代(包括Eden和两个Survivor区)与年老代的比值(除去持久代)。设置为4,则年轻代与年老代所占比值为1:4,年轻代占整个堆栈的1/5
-XX:SurvivorRatio=4:设置年轻代中Eden区与Survivor区的大小比值。设置为4,则两个Survivor区与一个Eden区的比值为2:4,一个Survivor区占整个年轻代的1/6
-XX:MaxTenuringThreshold=0:设置垃圾最大年龄。如果设置为0的话,则年轻代对象不经过Survivor区,直接进入年老代。对于年老代比较多的应用,可以提高效率。
如果将此值设置为一个较大值,则年轻代对象会在Survivor区进行多次复制,这样可以增加对象再年轻代的存活时间,增加在年轻代即被回收的概率。

吞吐量优先的并行收集器

-XX:+UseParallelGC:选择垃圾收集器为并行收集器。此配置仅对年轻代有效
-XX:ParallelGCThreads=20:配置并行收集器的线程数,即:同时多少个线程一起进行垃圾回收。此值最好配置与处理器数目相等
-XX:+UseParallelOldGC:配置年老代垃圾收集方式为并行收集
-XX:MaxGCPauseMillis=100:设置每次年轻代垃圾回收的最长时间,如果无法满足此时间,JVM会自动调整年轻代大小,以满足此值
-XX:+UseAdaptiveSizePolicy:设置此选项后,并行收集器会自动选择年轻代区大小和相应的Survivor区比例,以达到目标系统规定的最低相应时间或者收集频率等,此值建议使用并行收集器时,一直打开

java -Xmx20m -Xms20m -Xmn10m  -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseParallelOldGC -XX:+PrintGCDetails -cp . ThreadTest

 import java.util.HashSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ThreadTest {
public static void main (String[] args) {
final HashSet hs = new HashSet();
ExecutorService executorService = Executors.newFixedThreadPool(10);
final Runnable command = new Runnable() {
@Override
public void run() {
while ( true ) {
try {
Thread.sleep( 100 );
} catch (InterruptedException e) {
e.printStackTrace();
}
hs.add( new Object[1024 * 10]);
}
}
};
executorService.execute(command);
}
}
[GC (Allocation Failure) [PSYoungGen: 8156K->1016K(9216K)] 8156K->7626K(19456K), 0.0071395 secs] [Times: user=0.00 sys=0.01, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 1016K->0K(9216K)] [ParOldGen: 6610K->7556K(10240K)] 7626K->7556K(19456K), [Metaspace: 2637K->2637K(1056768K)], 0.0553513 secs] [Times: user=0.11 sys=0.00, real=0.05 secs]
[Full GC (Ergonomics) [PSYoungGen: 8156K->5562K(9216K)] [ParOldGen: 7556K->10111K(10240K)] 15712K->15674K(19456K), [Metaspace: 2643K->2643K(1056768K)], 0.0361699 secs] [Times: user=0.06 sys=0.00, real=0.03 secs]
[Full GC (Ergonomics) [PSYoungGen: 8158K->8125K(9216K)] [ParOldGen: 10111K->10111K(10240K)] 18270K->18237K(19456K), [Metaspace: 2643K->2643K(1056768K)], 0.0401159 secs] [Times: user=0.08 sys=0.00, real=0.04 secs]
[Full GC (Ergonomics) [PSYoungGen: 8192K->8165K(9216K)] [ParOldGen: 10111K->10111K(10240K)] 18303K->18277K(19456K), [Metaspace: 2643K->2643K(1056768K)], 0.0298122 secs] [Times: user=0.05 sys=0.00, real=0.03 secs]
[Full GC (Ergonomics) [PSYoungGen: 8192K->8165K(9216K)] [ParOldGen: 10231K->10231K(10240K)] 18423K->18397K(19456K), [Metaspace: 2643K->2643K(1056768K)], 0.0359396 secs] [Times: user=0.07 sys=0.01, real=0.04 secs]
[Full GC (Allocation Failure) [PSYoungGen: 8165K->8165K(9216K)] [ParOldGen: 10231K->10231K(10240K)] 18397K->18397K(19456K), [Metaspace: 2643K->2643K(1056768K)], 0.0313513 secs] [Times: user=0.06 sys=0.00, real=0.03 secs]
Exception in thread "pool-1-thread-1" [Full GC (Ergonomics) [PSYoungGen: 8192K->0K(9216K)] [ParOldGen: 10234K->255K(10240K)] 18426K->255K(19456K), [Metaspace: 2667K->2667K(1056768K)], 0.0116908 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
java.lang.OutOfMemoryError: Java heap space
        at ThreadTest$1.run(ThreadTest.java:18)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Heap
 PSYoungGen      total 9216K, used 443K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 5% used [0x00000000ff600000,0x00000000ff66ef10,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
  to   space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
 ParOldGen       total 10240K, used 255K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 2% used [0x00000000fec00000,0x00000000fec3feb8,0x00000000ff600000)
 Metaspace       used 2683K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 292K, capacity 386K, committed 512K, reserved 1048576K

jinfo 31367

VM Flags:
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=20971520 -XX:MaxHeapSize=20971520 -XX:MaxNewSize=10485760 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=10485760 -XX:OldSize=10485760 -XX:ParallelGCThreads=20 -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:+UseParallelOldGC
Command line: -Xmx20m -Xms20m -Xmn10m -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseParallelOldGC -XX:+PrintGCDetails

jmap -histo 31367

 num     #instances         #bytes  class name
----------------------------------------------
1: 645 4861968 [Ljava.lang.Object;
2: 443 493928 [I
3: 1677 152136 [C

响应时间优先的收集器配置

-XX:+UseConcMarkSweepGC:设置年老代为并发收集
-XX:+UseParNewGC:设置年轻代为并行收集
-XX:CMSFullGCsBeforeCompaction:由于并发收集器不对内存空间进行压缩、整理,所以运行一段时间以后会产生“碎片”,使得运行效率降低。此值设置运行多少次GC以后对内存空间进行压缩、整理。
-XX:+UseCMSCompactAtFullCollection:打开对年老代的压缩。可能会影响性能,但是可以消除碎片

java -Xmx20m -Xms20m -Xmn10m -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+PrintGCDetails -cp . ThreadTest

[GC (Allocation Failure) [ParNew: 8156K->1023K(9216K), 0.0515080 secs] 8156K->7627K(19456K), 0.0515731 secs] [Times: user=0.10 sys=0.00, real=0.05 secs]
[GC (CMS Initial Mark) [1 CMS-initial-mark: 6603K(10240K)] 7667K(19456K), 0.0015729 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[CMS-concurrent-mark-start]
[CMS-concurrent-mark: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-preclean-start]
[CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (CMS Final Remark) [YG occupancy: 1064 K (9216 K)][Rescan (parallel) , 0.0120069 secs][weak refs processing, 0.0000126 secs][class unloading, 0.0002688 secs][scrub symbol table, 0.0004252 secs][scrub string table, 0.0001109 secs][1 CMS-remark: 6603K(10240K)] 7667K(19456K), 0.0129012 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[CMS-concurrent-sweep-start]
[CMS-concurrent-sweep: 0.000/0.000 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[CMS-concurrent-reset-start]
[CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
....
[CMS-concurrent-mark-start]
[CMS-concurrent-mark: 0.002/0.002 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-preclean-start]
[CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-abortable-preclean-start]
[CMS-concurrent-abortable-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (CMS Final Remark) [YG occupancy: 9197 K (9216 K)][Rescan (parallel) , 0.0141205 secs][weak refs processing, 0.0000115 secs][class unloading, 0.0003192 secs][scrub symbol table, 0.0004989 secs][scrub string table, 0.0001190 secs][1 CMS-remark: 10214K(10240K)] 19412K(19456K), 0.0151344 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
[CMS-concurrent-sweep-start]
[CMS-concurrent-sweep: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[CMS-concurrent-reset-start]
[CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
^CHeap
par new generation total 9216K, used 9199K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
eden space 8192K, 100% used [0x00000000fec00000, 0x00000000ff400000, 0x00000000ff400000)
from space 1024K, 98% used [0x00000000ff500000, 0x00000000ff5fbcc0, 0x00000000ff600000)
to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)
concurrent mark-sweep generation total 10240K, used 10214K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
Metaspace used 2685K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 292K, capacity 386K, committed 512K, reserved 1048576K

10. 线程池相关

 package io.netty.example.myTest;

 import java.util.concurrent.*;

 public class ThreadTest {
public static void main (String[] args) {
// 先2,然后queue,满了之后运行线程增加到max
//针对本例子,先创建2个线程,t1,t2进入线程池之后直接执行,t3加入阻塞队列,队列满,t4到来线程池扩张线程到max运行t4,t5到来无法处理,进入到reject
ExecutorService executorService = new ThreadPoolExecutor(2, 3,
0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
i++;
i--;
}
}
}, "1-test");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
i++;
i--;
}
}
}, "2-test");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
i++;
i--;
}
}
}, "3-test");
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
i++;
i--;
}
}
}, "4-test");
Thread t5 = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
i++;
i--;
}
}
}, "5-test");
executorService.execute(t1);
executorService.execute(t2);
executorService.execute(t3);
executorService.execute(t4);
executorService.execute(t5);
}
}

由于先创建2个线程,t1,t2进入线程池之后直接执行,t3加入阻塞队列,队列满,t4到来线程池扩张线程到max运行t4,t5到来无法处理,进入到reject,所以运行结果如下

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task Thread[5-test,5,main] rejected from java.util.concurrent.ThreadPoolExecutor@27716f4[Running, pool size = 3, active threads = 3, queued tasks = 1, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at io.netty.example.myTest.ThreadTest.main(ThreadTest.java:66)

如果new ArrayBlockingQueue<Runnable>(1) 改为 new LinkedBlockingQueue<Runnable>(), 则缓存队列无界,不会报上面的错

11. CPU饱和

同样用上面的例子,改为无界队列

前面两个线程T1/T2在运行
"pool-1-thread-2" #14 prio=5 os_prio=0 tid=0x00007f43c40e9800 nid=0x7c4a runnable [0x00007f43aed13000]
java.lang.Thread.State: RUNNABLE
at ThreadTest$2.run(ThreadTest.java:24)
at java.lang.Thread.run(Thread.java:748)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748) "pool-1-thread-1" #13 prio=5 os_prio=0 tid=0x00007f43c40e7800 nid=0x7c49 runnable [0x00007f43aee14000]
java.lang.Thread.State: RUNNABLE
at ThreadTest$1.run(ThreadTest.java:14)
at java.lang.Thread.run(Thread.java:748)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

[root@izm5e8p93wtcdi53pzqhi7z test]# cat /proc/cpuinfo 可以看到在两个CPU的情况下,%CPU最高达到200%

top -Hp 31805

java 线程状态相关测试的更多相关文章

  1. Java线程状态Jstack线程状态BLOCKED/TIMED_WAITING/WAITING解释

    一.线程5种状态 新建状态(New) 新创建了一个线程对象. 就绪状态(Runnable) 线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获 ...

  2. Java线程状态转换

    前言:对于Java线程状态方面的知识点,笔者总感觉朦朦胧胧,趁着最近整理资料,将Java线程状态方面的知识点总结归纳,以便加深记忆. 1.Java线程状态值 在Thread类源码中通过枚举为线程定义了 ...

  3. Java线程状态及 wait、sleep、join、interrupt、yield等的区别

    Java中的线程状态(详见Java线程状态及转换-MarchOn): wait:Object类的实例方法,释放CPU执行权,进入等待状态,直到  被中断.被拥有该对象锁的线程唤醒(notify或not ...

  4. Java线程状态切换以及核心方法

    1.Java线程状态 1.1 线程主要状态 ①初始(NEW):新创建了一个线程对象,但还没有调用start()方法.②运行(RUNNABLE):Java线程中将就绪(ready)和运行中(runnin ...

  5. 从源码看java线程状态

    关于java线程状态,网上查资料很混乱,有的说5种状态,有的说6种状态,初学者搞不清楚这个线程状态到底是怎么样的,今天我讲一下如何看源码去解决这个疑惑. 直接上代码: public class Thr ...

  6. 面试官:都说阻塞 I/O 模型将会使线程休眠,为什么 Java 线程状态却是 RUNNABLE?

    摘要: 原创出处 https://studyidea.cn 「公众号:程序通事 」欢迎关注和转载,保留摘要,谢谢! 使用 Java 阻塞 I/O 模型读取数据,将会导致线程阻塞,线程将会进入休眠,从而 ...

  7. Java线程状态、线程start方法源码、多线程、Java线程池、如何停止一个线程

    下面将依次介绍: 1. 线程状态.Java线程状态和线程池状态 2. start方法源码 3. 什么是线程池? 4. 线程池的工作原理和使用线程池的好处 5. ThreadPoolExecutor中的 ...

  8. JVM探秘:jstack查看Java线程状态

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. jstack命令可以打印Java进程的各个线程堆栈跟踪信息,可以用来查看Java中各个 ...

  9. 浅谈 Java线程状态转换及控制

    线程的状态(系统层面) 一个线程被创建后就进入了线程的生命周期.在线程的生命周期中,共包括新建(New).就绪(Runnable).运行(Running).阻塞(Blocked)和死亡(Dead)这五 ...

随机推荐

  1. 廖雪峰Java1-3流程控制-5循环

    while循环 while循环首先判断条件: 条件满足时循环:条件不满足时退出循环 如果一开始条件就不满足,一次都不循环.如while false int sum = 0; int n = 1; wh ...

  2. [UE4]键盘鼠标输入事件

    然后在角色的事件视图就可以使用预先定义好的事件

  3. Redis禁用持久化功能的设置

    原文转载至:https://www.cnblogs.com/rangeon/p/7067618.html 用过Redis的朋友都知道,这玩意有个比较强大的功能叫做持久化,就是在结束服务的时候把缓存中的 ...

  4. Java中涉及线程和并发相关的内容

    1:线程池 与每次需要时都创建线程相比,线程池可以降低创建线程的开销,这也是因为线程池在线程执行结束后进行的是回收操作,而不是真正的 销毁线程. 2:ReentrantLock ReentrantLo ...

  5. postgresql数据库常用操作命令及SQL语言

    (1)登录 peng@peng-virtual-machine:~$ sudo -u postgres psql 以用户postgres身份登录,postgres为用户名,可有多个用户,登录时会要求输 ...

  6. $.extend与$.fn.extend()

    很多情况下,用户需要对jQuery插件进行二次开发,那么我们来看看JQ原开发者为我们提供的两种扩展插件的方式如下: 1.类别类:相当于为jquery扩展一个类,比如现在我要扩展一个简单的想加的功能函数 ...

  7. VS2008自定义快捷键设置

    点[Keyboard..]

  8. 光伏电池测控系统python代码

    '''硬件keithley万用表和程控电源visa是VXIplug&play系统联盟制定的一套标准.python实现VISA,形成pyviva模块'''###IV测试系统的部分程序代码 fro ...

  9. css实战第三天小结

    1.谈一谈对层级的理解: 如果对两个并列的子元素都设置了相对于同一个父元素(如果没有设置父元素那么默认相对于浏览器而言)进行了定位(相对定位),则这两个都具有相同的层级(默认为0),他们的trbl也默 ...

  10. python 之 json 与pickle 模块

    序例化:将对象转换为可通过网络传输或可以存储到本地磁盘的数据格式(如:XML.JSON或特定格式的字节串)的过程称为序列化:反之,则称为反序列化. 1.[JSON] import json dic={ ...