public static void main(String[] args) {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
for (ThreadInfo threadInfo : threadInfos) {
System.out.println("["+threadInfo.getThreadId()+"]"+" "+threadInfo.getThreadName());
}
}
/**
* [6] Monitor Ctrl-Break
* [5] Attach Listener
* [4] Signal Dispatcher
* [3] Finalizer
* [2] Reference Handler
* [1] main
*/可以看出我们只是启动了一个main方法,java虚拟机也给我们启动了6个线程,所以我们说java里的程序天生都是多线程的private static class testThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 10 ; i++) {
System.out.println(Thread.currentThread().getName()+" 执行了 "+i);
}
}
}
//测试
public static void main(String[] args) {
new testThread().start();
for (int i = 0; i < 10 ; i++) {
System.out.println(Thread.currentThread().getName()+" 执行了 "+i);
}
}
/**执行结果
main 执行了 0
Thread-0 执行了 0
main 执行了 1
main 执行了 2
main 执行了 3
Thread-0 执行了 1
main 执行了 4
Thread-0 执行了 2
main 执行了 5
Thread-0 执行了 3
main 执行了 6
Thread-0 执行了 4
main 执行了 7
Thread-0 执行了 5
Thread-0 执行了 6
main 执行了 8
*/可以看到main线程和thread0线程的for循环交替执行,线程启动成功。此实例程序中Thread1类继承了Thread类并重写了run方法,在run方法中我们打印出当前执行的线程名称为哪个线程正在执行了,执行了多少次,使用成员变量i来记录执行的次数。在main函数中我们创建了此线程的实例对象,并通过start方法启动了这个个线程,执行时大家可以看到线程是以抢占式的方式运行。虽然只创建了1个线程实例,实际上共有2个线程在运行,还有main方法代表的主线程的线程执行体。此外,我们还学习了线程的两个方法:
1.Thread.currentThread(),是Thread类的静态方法,该方法总是返回当前正在执行的线程对象。
2.getName():该方法是Thread类的实例方法,该方法返当前正在执行的线程的名称。在默认情况下,主线程的名称为main,用户启动的多线程的名称依次为Thread-0,Thread-1,Thread-3..Thread-n等。2.实现Runnable接口private static class testRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10 ; i++) {
System.out.println(Thread.currentThread().getName()+" 执行了 "+i);
}
}
}
//测试
public static void main(String[] args) {
new Thread(new testRunnable()).start();
for (int i = 0; i < 10 ; i++) {
System.out.println(Thread.currentThread().getName()+" 执行了 "+i);
}
}这里尤其是注意:main函数中名没有直接执行Thread2的run方法,而是将Thread2填入到了Thread中,使用start方法来启动。Runnable实现类里包含run方法,仅仅作为线程执行体,而实际的线程对象依然是Thread实例对象,Thread为真正创建线程的对象。private static class testCallable implements Callable<String>{
@Override
public String call() throws Exception {
System.out.println("I am implements Callable");
return "CallResult";
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> futureTask = new FutureTask<>(new testCallable());
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
/** 执行结果
I am implements Callable
CallResult
*/三、如何使java里面的线程安全停止工作public void Thread.interrupt() //中断线程
public boolean Thread.isInterrupted() //判断线程是否中断
public static boolean Thread.interrupted() //判断是否被中断,并清除当前中断状态Thread.interrupt()方法是一个实例方法,它通知目标线程中断,也是设置中断标志位。中断标志位表示当前线程已经被中断了。public static void main(String[] args) throws ExecutionException, InterruptedException {
Thread thread = new Thread(){
@Override
public void run() {
//中断处理逻辑
while (true){
System.out.println("The thread is waiting for interrupted!");
/*if (Thread.currentThread().isInterrupted()){
System.out.println("The thread is interrupted!");
break;
}*/
}
}
};
thread.start();
thread.interrupt();//中断线程
}
/**
The thread is waiting for interrupted!
The thread is waiting for interrupted!
The thread is waiting for interrupted!
The thread is waiting for interrupted!
The thread is waiting for interrupted!
The thread is waiting for interrupted!
.....
*/可以看到如果单独的使用interrupt()方法去中断线程的话,该线程其实并没有中断public static void main(String[] args) throws ExecutionException, InterruptedException {
Thread thread = new Thread(){
@Override
public void run() {
while (true){
System.out.println("The thread is waiting for interrupted!");
if (Thread.currentThread().isInterrupted()){
System.out.println("The thread is interrupted!");
break;
}
}
}
};
thread.start();
thread.interrupt();//中断线程
}
/** 执行结果
The thread is waiting for interrupted!
The thread is interrupted!
*/注意: 当线程A调用线程B的中断方法时,如果B的线程正在进行阻塞(例如 sleep、wait 等),那么此时就会抛出 InterruptExcpetion,同时B线程的中断状态会被重置false,此时两种处理办法:public static void main(String[] args) throws ExecutionException, InterruptedException {
Thread thread = new Thread(){
@Override
public void run() {
while (true){
try {
sleep(2000);
} catch (InterruptedException e) {
interrupt();
e.printStackTrace();
}
if (Thread.currentThread().isInterrupted()){
System.out.println("The thread is interrupted!");
break;
}
}
}
};
thread.start();
thread.interrupt();//中断线程
}四、线程的生命周期ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。