欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Thread系列:run()方法和start()方法有什么区别

程序员文章站 2024-03-26 12:03:05
...

系统通过调用线程类的start()方法来启动一个新线程,此时线程处于可运行状态,而非运行状态。这个线程可以被JVM来调度执行。在调度过程中,JVM通过调用线程类的run()方法来完成操作,run()执行结束,该线程也会终止。

直接调用run()方法,只是把run()方法当做一个普通的函数了调用,程序中仍然只有一个主线程。

也就是说,start()能够异步调用run()方法,直接调用run()方法是同步的,要实现多线程的目的只用调用start()方法。

实例1: 演示一下,调用start()方法会创建一个新的线程,当run()方法执行结束,新线程终止。

public class UP1013 {

    public static void main(String[] args) {
	System.out.println("*********启动 检测有几个线程*******");
	Thread mainThread = Thread.currentThread();
	ThreadGroup mainThreadThreadGroup = mainThread.getThreadGroup();
	// 获取线程组中的线程。
	int count = mainThreadThreadGroup.activeCount(); //
	Thread[] threads = new Thread[count];
	mainThreadThreadGroup.enumerate(threads, true);  //enumerate方法用来将ThreadGroup线程组中的active线程全部复制到Thread类型的数组中,并且返回数组中元素个数,即线程组中active线程数量
	System.out.println("线程数量 = " + count);
	Stream.of(threads).forEach(thread -> System.out.println("Name = " +thread.getName()));
	// 调用线程的start()方法,创建一个新的线程
	Thread thread = new Thread(new myThread(), "myThread");
	thread.start();
	try {
	    thread.join();
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	System.out.println("*********run() end 重新检测有几个线程*******");
	count = mainThreadThreadGroup.activeCount();
	Thread[] threads3 = new Thread[count];
	mainThreadThreadGroup.enumerate(threads3, true);
	System.out.println("线程数量 = " + count);
	Stream.of(threads3).forEach(thread3 -> System.out.println("Name = " + thread3.getName()));
    }

}

class myThread implements Runnable {

    @Override
    public void run() {
	System.out.println("*********run() ing 重新检测有几个线程*******");
	Thread mainThread = Thread.currentThread();
	ThreadGroup mainThreadThreadGroup = mainThread.getThreadGroup();
	// 获取线程组中的线程。
	int count = mainThreadThreadGroup.activeCount(); 
	Thread[] threads = new Thread[count];
	mainThreadThreadGroup.enumerate(threads, true);  
	System.out.println("线程数量 = " + count);
	Stream.of(threads).forEach(thread -> System.out.println("Name = " +thread.getName()));
	try {
	    Thread.sleep(5 * 1000); // 等待5s
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
	System.out.println("myThread run end");
    }

}

运行结果
Thread系列:run()方法和start()方法有什么区别
实例2: 演示一下,直接调用run()方法,只有主线程。将thread.start();调整为thread.run();

public class UP1013 {

    public static void main(String[] args) {
	System.out.println("*********启动 检测有几个线程*******");
	Thread mainThread = Thread.currentThread();
	ThreadGroup mainThreadThreadGroup = mainThread.getThreadGroup();
	// 获取线程组中的线程。
	int count = mainThreadThreadGroup.activeCount(); //
	Thread[] threads = new Thread[count];
	mainThreadThreadGroup.enumerate(threads, true);  //enumerate方法用来将ThreadGroup线程组中的active线程全部复制到Thread类型的数组中,并且返回数组中元素个数,即线程组中active线程数量
	System.out.println("线程数量 = " + count);
	Stream.of(threads).forEach(thread -> System.out.println("Name = " +thread.getName()));
	// 调用线程的start()方法,创建一个新的线程
	Thread thread = new Thread(new myThread(), "myThread");
	thread.run();
	System.out.println("*********run() end 重新检测有几个线程*******");
	count = mainThreadThreadGroup.activeCount();
	Thread[] threads3 = new Thread[count];
	mainThreadThreadGroup.enumerate(threads3, true);
	System.out.println("线程数量 = " + count);
	Stream.of(threads3).forEach(thread3 -> System.out.println("Name = " + thread3.getName()));
    }

}

class myThread implements Runnable {

    @Override
    public void run() {
	System.out.println("*********run() ing 重新检测有几个线程*******");
	Thread mainThread = Thread.currentThread();
	ThreadGroup mainThreadThreadGroup = mainThread.getThreadGroup();
	// 获取线程组中的线程。
	int count = mainThreadThreadGroup.activeCount(); 
	Thread[] threads = new Thread[count];
	mainThreadThreadGroup.enumerate(threads, true);  
	System.out.println("线程数量 = " + count);
	Stream.of(threads).forEach(thread -> System.out.println("Name = " +thread.getName()));
	try {
	    Thread.sleep(5 * 1000); // 等待5s
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
	System.out.println("myThread run end");
    }

}

运行结果:

Thread系列:run()方法和start()方法有什么区别
实例3: 演示一下,调用start()方法异步执行run()方法

public class UP1013 {

    public static void main(String[] args) {
	Thread thread = new Thread(new myThread());
	thread.start();
	System.out.println("finish");
    }

}

class myThread implements Runnable {
    
    @Override
    public void run() {
	System.out.println("myThread run start");
	try {
	    Thread.sleep(5 * 1000); // 等待5s
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
	System.out.println("myThread run end");
    }

}

运行结果:

finish
myThread run start
myThread run end

main()方法中的System.out.println(“finish”);语句,不需要等待thread.start()运行结果就可以执行,顾是异步。

实例4: 演示一下,直接调用run()方法的执行是同步的。

public class UP1013 {

    public static void main(String[] args) {
	Thread thread = new Thread(new myThread());
	thread.run();
	System.out.println("finish");
    }

}

class myThread implements Runnable {
    
    @Override
    public void run() {
	System.out.println("myThread run start");
	try {
	    Thread.sleep(5 * 1000); // 等待5s
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
	System.out.println("myThread run end");
    }

}

运行结果:

myThread run start
myThread run end
finish

main()方法中的System.out.println(“finish”);语句,只有等待thread.run()调用结束才可以执行,顾是同步。