线程 进程 多线程:
0 线程创建方法代码:
package thread; /** * 线程须知: 线程的创建方式 thread.start(); 表示启动线程,并将这个线程添加到线程池中, 启动后分配到CPU时间片后,代码自动调用这个线程的run()方法 * run()方法内执行的是线程体 * * Thread 主要看的源码: * public Thread(Runnable target) { init(null, target, "Thread-" + nextThreadNum(), 0); } public void run() { if (target != null) { target.run(); } } * @author zm * */ public class CreateThread { public static void main(String[] args) { // 创建方式1 继承父类 重写父类方法run(), 让线程执行子类重写后的线程体 // 分支线程执行 ChildThread childThread = new ChildThread(); childThread.start(); // 创建方式2, 面相对象思想,将线程体交给Thread,让Thread调用runnable的run() 干活的人我给你了,具体什么时候干让Thread来决定 MyRunnable myRunnable = new MyRunnable(); Thread child2Thread = new Thread(myRunnable); child2Thread.start(); // 看如下案例, 分析结果: 结果是 thread : 线程名 案例重写了 run()方法因此不会再调用Thread.run() ,自然不会执行 runnable对象的run() new Thread( new Runnable(){ public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("runnable :" + Thread.currentThread().getName()); } } }){ public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("thread :" + Thread.currentThread().getName()); } } }.start(); // 主线程执行 while(true) { try { Thread.sleep(700); System.out.println("main name is: " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } } class ChildThread extends Thread{ @Override public void run() { while(true) { try { Thread.sleep(700); System.out.println("ChildThread name is: " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } } class MyRunnable implements Runnable{ @Override public void run() { while(true) { try { Thread.sleep(700); System.out.println("ChildThread1 name is: " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
1 线程创建方法总结:
1 线程两种创建方法下 执行线程都是用 thread.start(), 将thread交给cpu等待时间段 2 线程启动,真正执行功能代码放在 run(){}中, 看Thread的run()源代码写法: /** 明确说明了线程真正执行体执行内容 * If this thread was constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object's <code>run</code> method is called; 如果传递runnable对象那么将执行runnable的run()方法 * otherwise, this method does nothing and returns. * <p> * Subclasses of <code>Thread</code> should override this method. 如果是Thread的话 那么需要重写此方法 * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */ public void run() { if (target != null) { target.run(); } } 3 如果一个类,既集成了Thread并重写了run() 同时又给这个类传递了runnable对象,那么执行的是重写Thread的run()方法,因此run方法重写没有机会在执行到 target != null target.run()2 脑图:
3 线程创建方式3--> 通过Runnable子类FutureTask,得到线程执行结果: 特点是 能够抛出异常 得到返回值
public static void main(String[] args) throws InterruptedException, ExecutionException { // TODO Auto-generated method stub FutureTask<String> ft = new FutureTask<String>(new Callable<String>() { @Override public String call() throws Exception { System.out.println("执行业务方法"); return "ok"; } }); Thread thread3 = new Thread(ft); thread3.start(); String result = ft.get(); System.out.println("线程创建方式3,得到执行结果为: " +result); }