ava 线程返回值,休眠,优先级,后台线程 示例代码
package org.rui.thread.basic; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * 从任务中产生返回值 Callable接口应用 * 也可以用isDone()来查询Future是否已经完成 * 也可以直接get()进行检查 * * @author lenovo * */ class TaskWithResult implements Callable<String> { private int id; public TaskWithResult(int id){this.id=id;} @Override public String call() throws Exception { return "result of TaskWotjResult"+id; } } public class CallableDemo { ///////////////// public static void main(String[] args) { ExecutorService exec=Executors.newCachedThreadPool(); ArrayList<Future<String>> result=new ArrayList<Future<String>>(); for(int i=0;i<10;i++) { result.add(exec.submit(new TaskWithResult(i))); } for(Future<String> fs:result) try { System.out.println(fs.get()); } catch (InterruptedException e) { e.printStackTrace(); return; } catch (ExecutionException e) { e.printStackTrace(); }finally { exec.shutdown(); } } } /** * output: result of TaskWotjResult0 result of TaskWotjResult1 result of TaskWotjResult2 result of TaskWotjResult3 result of TaskWotjResult4 result of TaskWotjResult5 result of TaskWotjResult6 result of TaskWotjResult7 result of TaskWotjResult8 result of TaskWotjResult9 */
package org.rui.thread.basic; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 线程优先级 * @author lenovo * */ public class SimpleProorities implements Runnable{ private int countDown=5; private volatile double d; //No optimization private int priority; public SimpleProorities(int priority) { this.priority=priority; } public String toString() { return Thread.currentThread()+" : "+countDown; } @Override public void run() { Thread.currentThread().setPriority(priority);//开头设置优先级 while(true) { for(int i=1;i<100000;i++) { //比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。 //任何其他值都更接近 e(即自然对数的底数)的 double 值。 d+=(Math.PI+Math.E)/(double)i; if(i%1000==0) Thread.yield(); } System.out.println(this); if(--countDown==0)return; } } public static void main(String[] args) { ExecutorService exec=Executors.newCachedThreadPool(); for(int i=0;i<5;i++) exec.execute(new SimpleProorities(Thread.MIN_PRIORITY)); exec.execute(new SimpleProorities(Thread.MAX_PRIORITY));//优先级高 exec.shutdown(); } }
package org.rui.thread.basic; import java.util.concurrent.TimeUnit; /** * 后台线程 * 是指在程序运行的时候在后台提供一种通用服务的线程 * @author lenovo * */ public class SimpleDeamons implements Runnable{ @Override public void run() { try { while(true) { TimeUnit.MILLISECONDS.sleep(100); System.out.println(Thread.currentThread()+" >>> "+this); } } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { for(int i=0;i<10;i++) { Thread daemon=new Thread(new SimpleDeamons()); daemon.setDaemon(true);//设为后台 线程 daemon.start(); } System.out.println("all daemons started------"); try { //试试调整休眠时间 以观察这个行为 TimeUnit.MILLISECONDS.sleep(175); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * output: all daemons started------ Thread[Thread-8,5,main] >>> org.rui.thread.basic.SimpleDeamons@1e4f7c2 Thread[Thread-9,5,main] >>> org.rui.thread.basic.SimpleDeamons@10dc6b5 Thread[Thread-4,5,main] >>> org.rui.thread.basic.SimpleDeamons@1d0fafc Thread[Thread-6,5,main] >>> org.rui.thread.basic.SimpleDeamons@c9d92c Thread[Thread-0,5,main] >>> org.rui.thread.basic.SimpleDeamons@145f0e3 Thread[Thread-2,5,main] >>> org.rui.thread.basic.SimpleDeamons@1e4f7c2 Thread[Thread-5,5,main] >>> org.rui.thread.basic.SimpleDeamons@b8f8eb Thread[Thread-1,5,main] >>> org.rui.thread.basic.SimpleDeamons@d0af9b Thread[Thread-7,5,main] >>> org.rui.thread.basic.SimpleDeamons@f47396 Thread[Thread-3,5,main] >>> org.rui.thread.basic.SimpleDeamons@170bea5 */
package org.rui.thread.basic; import java.util.concurrent.ThreadFactory; /** *通过编写定制的threadFactory可以定制由Executor创建的线程的属性 (后台,优先级,名称) * @author lenovo * */ public class DaemonThreadFactory implements ThreadFactory{ @Override public Thread newThread(Runnable r) { Thread thread=new Thread(r); thread.setDaemon(true); return thread; } }
package org.rui.thread.basic; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * 这与普通的ThreadFactory的唯一差异就是它将后台状态全部设置为了true * 你现在可以用一个新的DaemonThreadFactory作为参数传递给executory.newCachedThreadPool() * @author lenovo * */ public class DeamonFromFactory implements Runnable{ @Override public void run() { try { while(true) { TimeUnit.MILLISECONDS.sleep(100); System.out.println(Thread.currentThread()+" >> "+this); } } catch (Exception e) { } } ///// public static void main(String[] args) throws InterruptedException { //每一个ExecutorService创建方法都被重载为接受一个threadfactory对象 //而这个对象将被用来创建新的线程 ExecutorService exec=Executors.newCachedThreadPool(new DaemonThreadFactory()); for(int i=0;i<10;i++) { exec.execute(new DeamonFromFactory()); } System.out.println("all daemons started"); TimeUnit.MILLISECONDS.sleep(500); } } /**output: all daemons started Thread[Thread-0,5,main] >> org.rui.thread.basic.DeamonFromFactory@8ab708 Thread[Thread-3,5,main] >> org.rui.thread.basic.DeamonFromFactory@9934d4 Thread[Thread-5,5,main] >> org.rui.thread.basic.DeamonFromFactory@f6ac0b Thread[Thread-4,5,main] >> org.rui.thread.basic.DeamonFromFactory@1c0f2e5 Thread[Thread-2,5,main] >> org.rui.thread.basic.DeamonFromFactory@13c7378 Thread[Thread-1,5,main] >> org.rui.thread.basic.DeamonFromFactory@54c4ad Thread[Thread-6,5,main] >> org.rui.thread.basic.DeamonFromFactory@5b8827 Thread[Thread-7,5,main] >> org.rui.thread.basic.DeamonFromFactory@d09ad3 Thread[Thread-8,5,main] >> org.rui.thread.basic.DeamonFromFactory@147c1db Thread[Thread-9,5,main] >> org.rui.thread.basic.DeamonFromFactory@82d37 Thread[Thread-0,5,main] >> org.rui.thread.basic.DeamonFromFactory@8ab708 Thread[Thread-5,5,main] >> org.rui.thread.basic.DeamonFromFactory@f6ac0b Thread[Thread-3,5,main] >> org.rui.thread.basic.DeamonFromFactory@9934d4 Thread[Thread-1,5,main] >> org.rui.thread.basic.DeamonFromFactory@54c4ad Thread[Thread-4,5,main] >> org.rui.thread.basic.DeamonFromFactory@1c0f2e5 Thread[Thread-2,5,main] >> org.rui.thread.basic.DeamonFromFactory@13c7378 Thread[Thread-6,5,main] >> org.rui.thread.basic.DeamonFromFactory@5b8827 Thread[Thread-8,5,main] >> org.rui.thread.basic.DeamonFromFactory@147c1db Thread[Thread-7,5,main] >> org.rui.thread.basic.DeamonFromFactory@d09ad3 Thread[Thread-9,5,main] >> org.rui.thread.basic.DeamonFromFactory@82d37 Thread[Thread-5,5,main] >> org.rui.thread.basic.DeamonFromFactory@f6ac0b Thread[Thread-1,5,main] >> org.rui.thread.basic.DeamonFromFactory@54c4ad Thread[Thread-0,5,main] >> org.rui.thread.basic.DeamonFromFactory@8ab708 Thread[Thread-3,5,main] >> org.rui.thread.basic.DeamonFromFactory@9934d4 Thread[Thread-7,5,main] >> org.rui.thread.basic.DeamonFromFactory@d09ad3 Thread[Thread-4,5,main] >> org.rui.thread.basic.DeamonFromFactory@1c0f2e5 Thread[Thread-2,5,main] >> org.rui.thread.basic.DeamonFromFactory@13c7378 Thread[Thread-6,5,main] >> org.rui.thread.basic.DeamonFromFactory@5b8827 Thread[Thread-8,5,main] >> org.rui.thread.basic.DeamonFromFactory@147c1db Thread[Thread-9,5,main] >> org.rui.thread.basic.DeamonFromFactory@82d37 Thread[Thread-1,5,main] >> org.rui.thread.basic.DeamonFromFactory@54c4ad Thread[Thread-5,5,main] >> org.rui.thread.basic.DeamonFromFactory@f6ac0b Thread[Thread-3,5,main] >> org.rui.thread.basic.DeamonFromFactory@9934d4 Thread[Thread-0,5,main] >> org.rui.thread.basic.DeamonFromFactory@8ab708 Thread[Thread-8,5,main] >> org.rui.thread.basic.DeamonFromFactory@147c1db Thread[Thread-6,5,main] >> org.rui.thread.basic.DeamonFromFactory@5b8827 Thread[Thread-2,5,main] >> org.rui.thread.basic.DeamonFromFactory@13c7378 Thread[Thread-4,5,main] >> org.rui.thread.basic.DeamonFromFactory@1c0f2e5 Thread[Thread-7,5,main] >> org.rui.thread.basic.DeamonFromFactory@d09ad3 Thread[Thread-9,5,main] >> org.rui.thread.basic.DeamonFromFactory@82d37 Thread[Thread-0,5,main] >> org.rui.thread.basic.DeamonFromFactory@8ab708 Thread[Thread-3,5,main] >> org.rui.thread.basic.DeamonFromFactory@9934d4 Thread[Thread-1,5,main] >> org.rui.thread.basic.DeamonFromFactory@54c4ad Thread[Thread-5,5,main] >> org.rui.thread.basic.DeamonFromFactory@f6ac0b Thread[Thread-8,5,main] >> org.rui.thread.basic.DeamonFromFactory@147c1db Thread[Thread-6,5,main] >> org.rui.thread.basic.DeamonFromFactory@5b8827 Thread[Thread-2,5,main] >> org.rui.thread.basic.DeamonFromFactory@13c7378 Thread[Thread-4,5,main] >> org.rui.thread.basic.DeamonFromFactory@1c0f2e5 Thread[Thread-7,5,main] >> org.rui.thread.basic.DeamonFromFactory@d09ad3 */
package org.rui.thread.basic; import java.util.concurrent.BlockingQueue; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 每个静态的executorService创建方法都被重载为接受一个threadfactory对象, * 而这个对象将被用来创建新的线程 * @author lenovo * */ public class DaemonThreadPoolExecutor extends ThreadPoolExecutor { public DaemonThreadPoolExecutor() { super(0,Integer.MAX_VALUE,60L,TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new DaemonThreadFactory()); } }
package org.rui.thread.basic; import java.util.concurrent.TimeUnit; /** * 可以通过调用isDaemon方法来确定线程是否是一个后台线程。如果是一个后台线程, * 那么创建的任何线程将被自动设置成后台线程,如下例所示 * @author lenovo * */ class Daemon implements Runnable { private Thread[] t=new Thread[10]; @Override public void run() { for(int i=0;i<t.length;i++) { t[i]=new Thread(new DaemonSpawn()); t[i].start(); System.out.println("daemonSpawn "+i+" started, "); } for(int i=0;i<t.length;i++) { System.out.println("t["+i+"].isDaemon()="+t[i].isDaemon()+" ,"); } /** * 对线程调度器 java线程机制的一部分,可以将cpu从一个线程转移给另一个线程 的一种建议 * 它在声明: 我已经执行完生命周期中最重要的部分了,此刻正是切换给其他任务执行一段时间的大好时机 * 完全是先择性的, * */ while(true) Thread.yield();//把控制权交给其它线程 } } class DaemonSpawn implements Runnable { @Override public void run() { while(true) Thread.yield(); } } //////////////////////////////////// public class Daemons { public static void main(String[] args) throws InterruptedException { Thread d=new Thread(new Daemon()); d.setDaemon(true); d.start(); System.out.println("d.isDaemon()="+d.isDaemon()+" , "); TimeUnit.MILLISECONDS.sleep(1); } } /** d.isDaemon()=true , daemonSpawn 0 started, daemonSpawn 1 started, daemonSpawn 2 started, daemonSpawn 3 started, daemonSpawn 4 started, daemonSpawn 5 started, daemonSpawn 6 started, daemonSpawn 7 started, daemonSpawn 8 started, daemonSpawn 9 started, t[0].isDaemon()=true , t[1].isDaemon()=true , t[2].isDaemon()=true , t[3].isDaemon()=true , t[4].isDaemon()=true , t[5].isDaemon()=true , t[6].isDaemon()=true , t[7].isDaemon()=true , t[8].isDaemon()=true , t[9].isDaemon()=true , */
package org.rui.thread.basic; import java.util.concurrent.TimeUnit; /** * 你就该意识到后台进程在不执行finally子句的情况下就会终止其run()方法 * @author lenovo * */ class ADeamon implements Runnable{ @Override public void run() { try { System.out.println("starting adaemon"); TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException e) { System.out.println("正在退出 经过 InterruptedException"); }finally { //这应该 run? System.out.println("this should always run?"); } } } public class DaemonDontRunFinally { //你将看到finally子句不会执行 如果注释t.setDaemon(true); 就会看到 public static void main(String[] args) { Thread t=new Thread(new ADeamon()); t.setDaemon(true); t.start(); //一旦main退出 jvm就会立即关闭后台线程 } } /**output: starting adaemon */
java 线程返回值,优先级,后台线程 示例代码,布布扣,bubuko.com
原文地址:http://blog.csdn.net/liangrui1988/article/details/35647441