一、如何调试和查看多线程程序?
命令行下输入:
$ jconsole
二、线程的生命周期?
NEW
RUNNABLE
RUNNING
BLOCKKED
TERMINATED
三、例子:
package com.javaconcurrencyprogramming.chapter1; import java.util.concurrent.TimeUnit; import static java.lang.Thread.sleep; /** * @description: java多线程的一个例子 * @author: * @create: **/ public class TryConcurrency { public static void main(String[] args) { /* 线程启动必须在前 */ /*new Thread(){ public void run(){ enjoyMusic(); } }.start(); browseNews(); */ //jdk8中的写法 new Thread(TryConcurrency::enjoyMusic).start(); browseNews(); } private static void browseNews() { for (;;){ System.out.println("Uh-huh, the good news."); sleep(1); } } private static void enjoyMusic(){ for (;;){ System.out.println("Uh-huh, the nice music."); sleep(1); } } private static void sleep(int seconds){ try { TimeUnit.SECONDS.sleep(seconds); } catch (InterruptedException e) { e.printStackTrace(); } } }