线程组:ThreadGroup
用法如下:new ThreadGroup(),括号里面要传线程组名称,且自带重写toString方法
java
class threadTest1 extends Thread{
public threadTest1(ThreadGroup group, String name) {
super(group, name);
}
}
public class threadGroup {
public static void main(String[] args){
ThreadGroup tg = new ThreadGroup("美女组");
System.out.println(tg.getName());
threadTest1 t1 = new threadTest1(tg,"悟空");
System.out.println(t1.getName()+"在"+t1.getThreadGroup().getName()+"中");
}
}
线程池:ThreadPool
java提供了一个工具类:Executors让我们获取对应线程池
开发中常用的线程池是固定大小的线程池:
static ExecutorService newFixedThreadPool(int nThreads) 创建一个线程池,该线程池重用固定数量的从共享无界队列中运行的线程。
☆只能是Runnable的实现类(无返回值)或者Callable的实现类(有返回值),
三种实现线程的方式:
1、继承Thread类,重写run方法,创建该类的对象并启动
2、实现Runnable接口,实现run方法,借助Thread类创建线程对象并启动
3、实现Callable接口,实现call方法,借助线程池使用
参考如下:
线程池启动:submit()
线程池关闭:shutdown()
java
class MyRunnable1 implements Runnable {
private String name;
public MyRunnable1(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
Thread t1 = Thread.currentThread();
t1.setName(name);
System.out.println(t1.getName() + " - " + i);
}
}
}
class MyCallable implements Callable<Object>{
private String name;
public MyCallable(String name) {
this.name = name;
}
@Override
public Object call() throws Exception {
for (int i = 1; i <= 10; i++) {
Thread t1 = Thread.currentThread();
t1.setName(name);
System.out.println(t1.getName() + " - " + i);
}
return null;
}
}
public class ThreadPoolDemo1 {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(new MyRunnable1("悟空")); // 相当于底层给我们将任务封装成了一个线程对象,并启动
pool.submit(new MyRunnable1("八戒")); // 相当于底层给我们将任务封装成了一个线程对象,并启动
//超过线程池大小的线程,也会执行,只不过是等待前面有线程执行结束后,才能执行
pool.submit(new MyRunnable1("和尚")); // 相当于底层给我们将任务封装成了一个线程对象,并启动
pool.submit(new MyCallable("悟空"));
pool.submit(new MyCallable("沙僧"));
pool.submit(new MyCallable("八戒"));
pool.submit(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
Thread t1 = Thread.currentThread();
System.out.println(t1.getName() + " - " + i);
}
}
});
pool.submit(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 1000; i++) {
System.out.println(Thread.currentThread().getName() + " - " + i);
}
}
});
//关闭线程池
// pool.shutdown();
}
}
定时器:Timer
定时任务:TimerTask 是一个抽象类,需要一个子类继承,或者直接写匿名内部类,需要重写run方法。
启动定时任务: new Timer().schedule(new TimerTask/子类,10000,2000),意思是:10秒后运行启动,启动后再每隔2秒启动一次。
关闭定时:new Timer().cancel
java
import java.util.Timer;
import java.util.TimerTask;
/*
定时器:Timer
定时任务:TimerTask
*/
public class TimerDemo {
public static void main(String[] args) {
//创建一个定时器
//public Timer()
Timer timer = new Timer();
//public void schedule(TimerTask task, long delay) 从运行开始,延迟多少毫秒后执行
// timer.schedule(new MyTask(timer), 10000);
//public void schedule(TimerTask task,long delay,long period) 从运行开始,延迟多少毫秒后执行,执行之后,每间隔多少毫秒执行一次
timer.schedule(new MyTask(),10000, 2000);
}
}
class MyTask extends TimerTask {
private Timer timer;
public MyTask() {
}
public MyTask(Timer timer) {
this.timer = timer;
}
@Override
public void run() {
System.out.println("砰! 爆炸了。。。。。");
if (timer != null) {
//关闭定时器
timer.cancel();
}
}
}