linux查看進程中線程及線程池


1 lsof -i :8084 查看端口程序

2.top -H -p #pid查看相關線程,找出最占資源的線程

3.printf '0x\n' #線程ID(即2中pid)得到線程ID的16進制

4.jstack #pid|grep -A 10 線程ID的16進制

 

查看進程中所有的線程

jstack #pid|grep nid=0x

查看gc相關的線程號

jstack #pid| grep nid=ox| grep GC | awk '{print "pid=" strtonum("0x"substr($7,7,12))"  " $0}'

查看自定義線程池(線程池的名字為xxxTreadPool)相關的線程號

jstack #pid|grep TreadPool- |awk '{print "pid=" strtonum("0x"substr($6,7,12))"  " $0}'

查看http-nio-線程

jstack #pid|grep http-nio- |awk '{print "pid=" strtonum("0x"substr($7,7,12))"  " $0}'

springboot web程序舉例

查看pid

 

 

top -H -p 122349

 

 

 

ps -T -p 122349

 

 

 

 spring boot web程序進程相關線程

jstack 122349|grep nid=0x|awk '{from=(index($0,"nid=0x")+6);to=(from+5);pid=substr($0,from);pid=substr(pid,0,5);pid=strtonum("0x"pid);print "pid="pid " "  substr($0,0,100)}'

 結果:黃色部分為自定義線程池,綠色部分為容器相關的線程,剩余是top能看到進程

pid=122941 "ScheduledThreadPool-1-thread-5" #43 prio=5 os_prio=0 tid=0x00007f3ba8004800 nid=0x1e03d waiting on pid=122940 "ScheduledThreadPool-1-thread-4" #42 prio=5 os_prio=0 tid=0x00007f3b98001000 nid=0x1e03c waiting on pid=122939 "ScheduledThreadPool-1-thread-3" #41 prio=5 os_prio=0 tid=0x00007f3ba8002800 nid=0x1e03b waiting on pid=122937 "ScheduledThreadPool-1-thread-2" #40 prio=5 os_prio=0 tid=0x00007f3ba8001000 nid=0x1e039 waiting on pid=122935 "CustomizedThreadPool-1-thread-3" #39 prio=5 os_prio=0 tid=0x00007f3bf8175000 nid=0x1e037 waiting on pid=122934 "CustomizedThreadPool-1-thread-2" #38 prio=5 os_prio=0 tid=0x00007f3bf8173000 nid=0x1e036 waiting on pid=122933 "CustomizedThreadPool-1-thread-1" #37 prio=5 os_prio=0 tid=0x00007f3bf818e000 nid=0x1e035 waiting on pid=122932 "ScheduledThreadPool-1-thread-1" #36 prio=5 os_prio=0 tid=0x00007f3bf818b800 nid=0x1e034 waiting on pid=122931 "FixedThreadPool-1-thread-3" #35 prio=5 os_prio=0 tid=0x00007f3bf8189800 nid=0x1e033 waiting on cond pid=122930 "FixedThreadPool-1-thread-2" #34 prio=5 os_prio=0 tid=0x00007f3bf8188000 nid=0x1e032 waiting on cond pid=122929 "FixedThreadPool-1-thread-1" #33 prio=5 os_prio=0 tid=0x00007f3bf8191000 nid=0x1e031 waiting on cond
pid=122583 "Attach Listener" #32 daemon prio=9 os_prio=0 tid=0x00007f3c2c001000 nid=0x1ded7 waiting on conditio pid=122350 "DestroyJavaVM" #31 prio=5 os_prio=0 tid=0x00007f3c74009000 nid=0x1ddee waiting on condition [0x0000 pid=122387 "http-nio-8084-Acceptor-0" #29 daemon prio=5 os_prio=0 tid=0x00007f3c74ef0000 nid=0x1de13 runnable [ pid=122386 "http-nio-8084-ClientPoller-1" #28 daemon prio=5 os_prio=0 tid=0x00007f3c75263800 nid=0x1de12 runnab pid=122385 "http-nio-8084-ClientPoller-0" #27 daemon prio=5 os_prio=0 tid=0x00007f3c750a1800 nid=0x1de11 runnab pid=122384 "http-nio-8084-exec-10" #26 daemon prio=5 os_prio=0 tid=0x00007f3c7527a800 nid=0x1de10 waiting on co pid=122383 "http-nio-8084-exec-9" #25 daemon prio=5 os_prio=0 tid=0x00007f3c7438a000 nid=0x1de0f waiting on con pid=122382 "http-nio-8084-exec-8" #24 daemon prio=5 os_prio=0 tid=0x00007f3c75087800 nid=0x1de0e waiting on con pid=122381 "http-nio-8084-exec-7" #23 daemon prio=5 os_prio=0 tid=0x00007f3c750f5000 nid=0x1de0d waiting on con pid=122380 "http-nio-8084-exec-6" #22 daemon prio=5 os_prio=0 tid=0x00007f3c750f7000 nid=0x1de0c waiting on con pid=122379 "http-nio-8084-exec-5" #21 daemon prio=5 os_prio=0 tid=0x00007f3c74cec800 nid=0x1de0b waiting on con pid=122378 "http-nio-8084-exec-4" #20 daemon prio=5 os_prio=0 tid=0x00007f3c74ef5000 nid=0x1de0a waiting on con pid=122377 "http-nio-8084-exec-3" #19 daemon prio=5 os_prio=0 tid=0x00007f3c7477b000 nid=0x1de09 waiting on con pid=122376 "http-nio-8084-exec-2" #18 daemon prio=5 os_prio=0 tid=0x00007f3c74e9e000 nid=0x1de08 waiting on con pid=122375 "http-nio-8084-exec-1" #17 daemon prio=5 os_prio=0 tid=0x00007f3c74f1f800 nid=0x1de07 waiting on con pid=122374 "NioBlockingSelector.BlockPoller-1" #16 daemon prio=5 os_prio=0 tid=0x00007f3c74f1e800 nid=0x1de06 r pid=122373 "container-0" #15 prio=5 os_prio=0 tid=0x00007f3c752d5800 nid=0x1de05 waiting on condition [0x00007f pid=122372 "Catalina-utility-2" #14 prio=1 os_prio=0 tid=0x00007f3c08406800 nid=0x1de04 waiting on condition [0 pid=122371 "Catalina-utility-1" #13 prio=1 os_prio=0 tid=0x00007f3c74cf5800 nid=0x1de03 waiting on condition [0
pid=122367 "Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00007f3c7422c000 nid=0x1ddff runnable [0x000000000
pid=122366 "C1 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007f3c7420f000 nid=0x1ddfe waiting on condit
pid=122365 "C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007f3c7420c800 nid=0x1ddfd waiting on condit
pid=122364 "C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f3c7420b000 nid=0x1ddfc waiting on condit
pid=122363 "C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f3c74208000 nid=0x1ddfb waiting on condit
pid=122362 "Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f3c74206800 nid=0x1ddfa runnable [0x000000
pid=122361 "Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f3c741d3800 nid=0x1ddf9 in Object.wait() [0x00007f
pid=122360 "Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f3c741d0800 nid=0x1ddf8 in Object.wait() 
pid=122359 "VM Thread" os_prio=0 tid=0x00007f3c741c7000 nid=0x1ddf7 runnable 
pid=122351 "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f3c7401e800 nid=0x1ddef runnable 
pid=122352 "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f3c74020000 nid=0x1ddf0 runnable 
pid=122353 "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f3c74022000 nid=0x1ddf1 runnable 
pid=122354 "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f3c74024000 nid=0x1ddf2 runnable 
pid=122355 "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00007f3c74025800 nid=0x1ddf3 runnable 
pid=122356 "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00007f3c74027800 nid=0x1ddf4 runnable 
pid=122357 "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00007f3c74029800 nid=0x1ddf5 runnable 
pid=122358 "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00007f3c7402b000 nid=0x1ddf6 runnable 
pid=122368 "VM Periodic Task Thread" os_prio=0 tid=0x00007f3c74239000 nid=0x1de00 waiting on condition

 附java代碼

package com.example.demo.threadmanage;

import com.example.demo.threadmanage.scheduler.NamedThreadFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.*;

/**
 * Created by Administrator on 2020/2/5.
 */
@Service
public class ExecutorsTest {
    public static void main(String[] args) {
        ExecutorsTest.threadPoolTest();
    }
    public static void threadPoolTest() {
        /**
         * 創建一個可緩存的線程池
         * 線程池為無限大,當執行當前任務時上一個任務已經完成,會復用執行上一個任務的線程,而不用每次新建線程
         */
        ExecutorService cachedThreadPool= Executors.newCachedThreadPool();
        //executeThreadPool(cachedThreadPool);

        /**
         * 創建一個可重用固定個數的線程池
         *因為線程池大小為3,每個任務輸出打印結果后sleep 2秒,所以每兩秒打印3個結果。
         *定長線程池的大小最好根據系統資源進行設置
         */
        ExecutorService fixedThreadPool=Executors.newFixedThreadPool(3,new NamedThreadFactory("FixedThreadPool"));
        executeThreadPool(fixedThreadPool);

        /**
         * 創建一個定長線程池,支持定時及周期性任務執行----延遲執行
         */
        ScheduledExecutorService scheduledThreadPool=Executors.newScheduledThreadPool(5,new NamedThreadFactory("ScheduledThreadPool"));
//        scheduledThreadPool.schedule(()->{
//            System.out.println("延遲2秒執行");
//        },2, TimeUnit.SECONDS);
        scheduledThreadPool.scheduleAtFixedRate(()->{
            System.out.println("延遲2秒后,每3秒執行一次"+getNowTime()+" 線程名稱:"+Thread.currentThread().getName());
        },2,3, TimeUnit.SECONDS);

        /**
         * 創建一個單線程化的線程池
         *創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,
         * 保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。
         */
        ExecutorService singleThreadExecutor=Executors.newSingleThreadExecutor();
//        executeThreadPool(singleThreadExecutor);

        /**
         * 自定義線程池ThreadPoolExecutor
         */
        //創建數組型緩沖等待隊列
        BlockingQueue<Runnable> bq = new ArrayBlockingQueue<>(10);
        ThreadPoolExecutor tpe=new ThreadPoolExecutor(3,6,50,TimeUnit.SECONDS,bq,new NamedThreadFactory("CustomizedThreadPool"));
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());
        tpe.execute(new TempThread());

//        tpe.shutdown();
    }

    public static String getNowTime(){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        return df.format(new Date());
    }
    public static void executeThreadPool(ExecutorService executorService){
        for (int i = 0; i < 10; i++) {
            System.out.println("thread order is =="+i);
            try{
                //sleep可明顯看到使用的是線程池里面以前的線程,沒有創建新線程
                Thread.sleep(1000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }

            executorService.execute(()->{
                System.out.println(Thread.currentThread().getName()+"正在被執行");
            });

        }
    }
}

class TempThread implements Runnable{
    @Override
    public void run() {
        try{
            //sleep可明顯看到使用的是線程池里面以前的線程,沒有創建新線程
            Thread.sleep(1000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"正在被執行 "+ExecutorsTest.getNowTime());

    }
}

線程工廠

package com.example.demo.threadmanage.scheduler;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2020/2/8.
 */
public class NamedThreadFactory implements ThreadFactory {
    private  final AtomicInteger poolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    public NamedThreadFactory(String name) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() :
                Thread.currentThread().getThreadGroup();
        if(null==name || "".equals(name.trim())){
            name="pool";
        }
        namePrefix = name+"-" +
                poolNumber.getAndIncrement() +
                "-thread-";
    }

    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r,
                namePrefix + threadNumber.getAndIncrement(),
                0);
        if (t.isDaemon())
            t.setDaemon(false);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM