码迷,mamicode.com
首页 > 编程语言 > 详细

java 线程、线程池基本应用示例代码回顾

时间:2014-06-29 22:56:42      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:线程池   线程   thread   文件系统   应用   

java 线程、线程池基本应用示例代码回顾

package org.rui.thread;
/**
 * 定义任务
 * 
 * @author lenovo
 *
 */
public class LiftOff implements Runnable {

	protected int countDown=10;
	private static int taskCount=0;
	private final int id=taskCount++;
	
	public LiftOff(){}
	public LiftOff(int countDown)
	{
		this.countDown=countDown;
	}
	
	public String status(){
		return "#"+id+"("
				+ (countDown>0?countDown:"liftoff!")+"),";
	}
	@Override
	public void run() {
		while(countDown-->0)
		{
			System.out.print(status());
			//机制的一部分,可以将cpu从一个线程转移给另一个线程 的一种建议  
			//它在声明: 我已经执行完生命周期中最重要的部分了,此刻正是切换给其他任务执行一段时间的大好时机  
			//为完全是先择性的,
			Thread.yield();//线程调度 
		}
		
	}
	
}

package org.rui.thread;
/**
 * Run  不是单独的线程驱动,它是在main中直接调用的  这里仍旧使用线程,即总是分配给main的那个线程
 * @author lenovo
 *
 */
public class MainThread {
	public static void main(String[] args) {
		LiftOff launch=new LiftOff();
		launch.run();
	}

}
/**
 *output:
 #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(liftoff!),
 */

package org.rui.thread;
/**
 * thread 类驱动LiftOff
 * @author lenovo
 *
 */
public class BasicThreads {
	public static void main(String[] args) {
		Thread t=new Thread(new LiftOff());
		t.start();
		System.out.println("waiting for liftoff()");
	}

}
/**output:
waiting for liftoff()
#0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(liftoff!),
*/

package org.rui.thread;
/**
 * 更多的线程 驱动LiftOff
 * @author lenovo
 *
 */
public class MoreBasicThreads {
	public static void main(String[] args) {
		for(int i=0;i<5;i++)
		{
			Thread t=new Thread(new LiftOff());
			t.start();
			System.out.println("waiting for liftoff()");
		}
		
	}

}
/**output:
waiting for liftoff()
#0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(liftoff!),waiting for liftoff()
waiting for liftoff()
#2(9),#2(8),#2(7),#2(6),#2(5),#2(4),#2(3),#2(2),#2(1),#2(liftoff!),waiting for liftoff()
waiting for liftoff()
#4(9),#1(9),#3(9),#4(8),#3(8),#4(7),#3(7),#4(6),#3(6),#4(5),#3(5),#4(4),#3(4),#4(3),#3(3),#4(2),#3(2),#4(1),#3(1),#4(liftoff!),#3(liftoff!),#1(8),#1(7),#1(6),#1(5),#1(4),#1(3),#1(2),#1(1),#1(liftoff!),
*/

package org.rui.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 使用executor
 * @author lenovo
 *
 */
public class CachedThreadPool {
	public static void main(String[] args) {
		//创建并返回设置有常用配置字符串的 ExecutorService 的方法。 
		/**
		 * newCachedThreadPool
		 * 创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。
		 */
		ExecutorService exec=Executors.newCachedThreadPool();
		
		for(int i=0;i<5;i++){
			exec.execute(new LiftOff());
		}
		//启动一次顺序关闭,执行以前提交的任务,但不接受新任务。
		exec.shutdown();//防止新的任务被提交到executor
		
	}

}
/**
 * OUTPUT:
 #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(liftoff!),
 #1(9),#3(9),#1(8),#3(8),#1(7),#3(7),#1(6),#3(6),#1(5),#3(5),#1(4),
 #3(4),#1(3),#3(3),#1(2),#3(2),#1(1),#3(1),#1(liftoff!),#3(liftoff!),
 #2(9),#2(8),#2(7),#2(6),#2(5),#2(4),#2(3),#2(2),
 #2(1),#2(liftoff!),#4(9),#4(8),#4(7),#4(6),#4(5),#4(4),#4(3),#4(2),#4(1),#4(liftoff!),
 */

package org.rui.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 使用executor
 * 
 * 在任何线程池中 现有线程在可能的情况下,都会自动复用
 * @author lenovo
 *
 */
public class FixedThreadPool {
	public static void main(String[] args) {
		
		
		/**
		 * 创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程。
		 */
		ExecutorService exec=Executors.newFixedThreadPool(5);
		for(int i=0;i<5;i++){
			exec.execute(new LiftOff());
		}
		exec.shutdown();
	}

}
/**
 * OUTPUT:
 #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(liftoff!),
 #1(9),#3(9),#1(8),#3(8),#1(7),#3(7),#1(6),#3(6),#1(5),#3(5),#1(4),
 #3(4),#1(3),#3(3),#1(2),#3(2),#1(1),#3(1),#1(liftoff!),#3(liftoff!),
 #2(9),#2(8),#2(7),#2(6),#2(5),#2(4),#2(3),#2(2),
 #2(1),#2(liftoff!),#4(9),#4(8),#4(7),#4(6),#4(5),#4(4),#4(3),#4(2),#4(1),#4(liftoff!),
 */

package org.rui.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 使用executor
 * 
 * 作为别一个示例 假设你有大量的线程 那它们运行的任务将使用文件系统,
 * 你可以用single。。。 来运行这些线程,以确保任意时刻在任何线程中都只有唯一的任务运行。
 * 这种方式 你不需要在共享资源上处理同步。。。。
 * @author lenovo
 *
 */
public class SingleThreadPool {
	public static void main(String[] args) {
		
		
		/**
		 * 创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程
		 */
		ExecutorService exec=Executors.newSingleThreadExecutor();
		for(int i=0;i<5;i++){
			exec.execute(new LiftOff());
		}
		exec.shutdown();
	}

}
/**
 * OUTPUT:
 #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(liftoff!),
 #1(9),#3(9),#1(8),#3(8),#1(7),#3(7),#1(6),#3(6),#1(5),#3(5),#1(4),
 #3(4),#1(3),#3(3),#1(2),#3(2),#1(1),#3(1),#1(liftoff!),#3(liftoff!),
 #2(9),#2(8),#2(7),#2(6),#2(5),#2(4),#2(3),#2(2),
 #2(1),#2(liftoff!),#4(9),#4(8),#4(7),#4(6),#4(5),#4(4),#4(3),#4(2),#4(1),#4(liftoff!),
 */


java 线程、线程池基本应用示例代码回顾,布布扣,bubuko.com

java 线程、线程池基本应用示例代码回顾

标签:线程池   线程   thread   文件系统   应用   

原文地址:http://blog.csdn.net/liangrui1988/article/details/35567879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!