java 线程、线程池基本应用演示样例代码回想

时间:2023-11-22 13:35:44

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!),
*/