1.线程池的创建
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//一个程序中只能有一个线程池
public class ThreadPoolDemo {
public static void main(String[] args) {
//public static ExecutorService newCachedThreadPool()创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。线程存活时间是60s
ExecutorService threadPool = Executors.newCachedThreadPool();
//void execute(Runnable command)在未来某个时间执行给定的命令
Runnable command = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"执行了");
}
};
//threadPool.execute(command);
//public static ExecutorService newSingleThreadExecutor()创建只有一个线程的线程池。如果线程不显示的关闭,线程将一直存活着
threadPool = Executors.newSingleThreadExecutor();
//threadPool.execute(command);
//public static ExecutorService newFixedThreadPool(int nThreads)创建一个可重用固定线程数的线程池。如果线程不显示的关闭,线程将一直存活着
threadPool = Executors.newFixedThreadPool(1);
//threadPool.execute(command);
//threadPool.execute(command);
//关闭线程池:void shutdown()启动一次顺序关闭,执行以前提交的任务,但不接受新任务
//threadPool.shutdown();//等整个程序退出的时候需要把线程池关闭
//new Thread(command).start();//我们自己创建线程
ThreadPoolUtils.execute(command);//pool-4-thread-1执行了
ThreadPoolUtils.shutdown();
ThreadPoolUtils.execute(command);//pool-5-thread-1执行了
}
}
2.工具包 子类对象交由线程执行
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolUtils {
private static ExecutorService threadPool = Executors.newCachedThreadPool();
/**
* 将Runnable子类对象交由线程池执行
*
* @param command
*/
public static void execute(Runnable command) {
// 判断线程池是否被关闭
if (threadPool.isShutdown()) {
// 如果线程池被关闭了,那么就新建一个线程池
threadPool = Executors.newCachedThreadPool();
execute(command);// 再次将任务交给线程池执行
} else {
threadPool.execute(command);
}
}
/**
* 关闭线程池
*/
public static void shutdown() {
threadPool.shutdown();
}
}
3.定时器的用法
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/*
* Timer:一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。
* TimerTask:由 Timer 安排为一次执行或重复执行的任务。
*/
public class TimerDemo {
public static void main(String[] args) {
test2();
}
private static void test2() {
//public Timer()创建一个新计时器。相关的线程不 作为守护程序运行
Timer timer = new Timer();
//protected TimerTask()创建一个新的计时器任务
TimerTask task = new TimerTask() {
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date());
System.out.println(time);
}
};
/*
* public void schedule(TimerTask task,
long delay,
long period)安排指定的任务从指定的延迟后开始进行重复的固定延迟执行
参数:
task - 所要安排的任务。
delay - 执行任务前的延迟时间,单位是毫秒。
period - 执行各后续任务之间的时间间隔,单位是毫秒。
*/
timer.schedule(task, 0, 1000);
}
private static void test() {
//public Timer()创建一个新计时器。相关的线程不 作为守护程序运行
Timer timer = new Timer();
//protected TimerTask()创建一个新的计时器任务
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("定时器执行的任务");
}
};
/*
* public void schedule(TimerTask task,long delay)安排在指定延迟后执行指定的任务。
参数:
* task - 所要安排的任务。
* delay - 执行任务前的延迟时间,单位是毫秒。
*/
timer.schedule(task, 1000);
//public boolean cancel()取消此计时器任务
//task.cancel();//终止的是任务
//public void cancel()终止此计时器,丢弃所有当前已安排的任务
timer.cancel();//终止此计时器
task = new TimerTask() {
@Override
public void run() {
System.out.println("定时器执行的任务");
}
};
timer.schedule(task, 1000);
//一个定时器任务能不能被执行多次?不行的
//timer.schedule(task, 3000);//Task already scheduled or cancelled
}
//安排在指定延迟后执行指定的任务。
public void schedule(Runnable task,long delay){
//创建一个线程
new Thread(new Runnable() {
@Override
public void run() {
//延迟指定delay的时间
try {
Thread.sleep(delay);
task.run();//run方法就执行在子线程中
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
4.网络传输之UDP
server
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//DatagramSocket:此类表示用来发送和接收数据包的套接字
public class Server {
public static void main(String[] args) throws Exception {
//1.创建Socket对象并监听指定的端口
DatagramSocket s = new DatagramSocket(8888);
//2.创建一个数据包容器用于存储数据,udp一次传输的数据包最大是64k
//public DatagramPacket(byte[] buf,int length) buf - 保存传入数据报的缓冲区。len - 要读取的字节数。
DatagramPacket p = new DatagramPack