任务和线程是不一样的,Java中Thread类自己不执行任何操做,它只驱动赋予它的任务,而Runnable才是定义任务的地方.java
1.查看Runnable源码,能够看到只有一个run()方法多线程
@FunctionalInterface public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */ public abstract void run(); }
2.使用Runnable,直接继承便可,这样就建立了一个任务.ide
public class MyRunnable implements Runnable{ @Override public void run() { int i=0; while(true) System.out.println(i++); } //暗示调度器该线程可让出资源了 Thread.yield(); }
1.Thread部分源码线程
/* * @see Runnable * @see Runtime#exit(int) * @see #run() * @see #stop() * @since JDK1.0 */ public class Thread implements Runnable { /* Make sure registerNatives is the first thing <clinit> does. */ private static native void registerNatives(); static { registerNatives(); }
2.能够看到Thread继承了Runnable接口,因此Thread建立任务也是用的run方法
3.代码能够这样写code
class MyThread extends Thread{ @Override public void run() { int count=10; while(count--!=0) System.out.print(count+" "); } }
1.不要误会,下面的方法并未启动一个线程,而是单纯的调用了实现Runnable接口的MyRunnable的run()方法,因此该任务的运行依旧被分派到该main线程中,不会独立运行在一个新线程里.对象
public class Main{ public static void main(String[] args){ Runnable runnable=new Runnable(); runnable.run(); } }
2.调用Thread的start(),Thread中记录了不少线程信息,包括线程名称和优先级等,以MyRunnable的实例做参传入Thread,而后start便可运行.继承
public static void main(String[] args){ Thread thread=new Thread(new MyRunnable); thread.start() }
3.在调用start()后,子线程不会由于主线程代码执行结束而中止.接口
public static void main(String[] args){ //常见执行器对象 ExecutorService executorService= Executors.newCachedThreadPool(); //向执行器中添加任务 executorService.execute(new MyRunnable()); //关闭向执行器中添加任务; executorService.shutdown();
建立执行器又三种类型可选,分别是newCachedThreadPool,newFixedThreadPool,newSingleThreadPool,区别以下资源
1.有些任务执行完后须要返回值,那么建立任务时能够经过实现Callale接口而实现该目的,Callable是一种具备类型参数的泛型,由于返回值是须要定义类型的.get
class Task implements Callable<String>{ @Override public String call() throws Exception { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); TimeUnit.MILLISECONDS.sleep(100); return "线程执行啦"; } }
2.而后在main()方法中接受返回值信息,对线程信息的管理能够用Future<String>
public static void main(String[] args){ ExecutorService executorService=Executors.newCachedThreadPool(); Future<String> future=executorService.submit(new Callable<String>(){ @Override public String call() throws Exception { return "执行啦"; } }); //判断是否完成 System.out.println(future.isDone()); //任务完成后,才会打印词条语句,不然会阻塞. System.out.println(future.get()); //判断是否完成 System.out.println(future.isDone()); } ----------输出---------- false 执行啦 true
3.程序运行到get()方法时会阻塞,当运行完后,主程序才会继续向后执行.
1.优先级的设置在run方法中
public void run(){ //例如将优先级设置为10 Thread.currentThread().setPriority(10) }
2.为了方便移植,建议将优先级设置为Thread中的三个常量.
1.设置后台线程语法 thread.setDaemon(true);要在start()以前.2.后台线程建立的线程也为后台线程.