public class StudentDemo {
public static void main(String[] args){
//创建资源对象
Sudent s = new Student();
//创建设置和获取对象
SetThread st = new SetThread(s);
GetThread gt = new GetThread(s);
//创建设置和获取线程
Thread t1 = new Thread(st);
Thread t2 = new Thread(gt);
//启动线程
t1.start();
t2.start();
}
}
13.线程组
A:常用功能
ThreadGroup(String name):通过该构造方法创建线程组
Thread(ThreadGroup group,Runnable target,String name):将线程添加到线程组,并为线程组命名
public final ThreadGroup getThreadGroup():获取该线程所属线程组
public final String getName():返回该线程组名称
Thread.currentThread().getThreadGroup().getName():获取正在运行的线程所属线程组的名称
B:代码演示
public class MyRunnable implements Runnable{
public void run(){
for(int x = 0; x < 100; x++){
System.out.println(Thread.currentThread().getName() + ":" + x);
}
}
}
public class ThreadGroupDemo{
public static void main(String[] args){
MyRunnable mr = new MyRunnable();
ThreadGroup tg = new ThreadGroup("线程组一");
Thread t1 = new Thread(tg,mr,"线程A");
Thread t2 = new Thread(tg,mr,"线程B");
B:多线程的线程池实现方式
a:实现Runnable接口
public class MyRunnable implements Runnable {
public void run(){
for(int x = 0;x < 100;x++){
System.out.println(Thread.currentThread().getName() + ":" + x);
}
}
}
public class ExecutorsDemo{
public static void main(String[] args){
//创建线程池,设置要创建的线程对象的数量
ExecutorService pool = Executors.newFixedThreadPool(2);
//执行Runnable对象代表的线程
pool.submit(new MyRunnable());
pool.submit(new MyRunnable());
//结束线程
pool.shutdown();
}
}
b:实现Callable<T>接口(多线程求和案例)
public class MyCollable implements Callable<Integer> {
private int number;
public MyCollable(int number){
this.number = number;
}
public Integer call() throws Exception {
int sum = 0;
for(int x = 1; x <= number; x++){
sum += x;
}
return sum;
}
}
public class CallableDemo{
public static void main(String[] args) throws InterruptedException, ExecutionException{
//创建线程池对象,设置数量
ExecutorService pool = Executors.newFixedThreadPool(2);
//执行Collable<T>对象代表的线程
Future<Integer> f1 = pool.submit(new MyCollable(100));
Future<Integer> f2 = pool.submit(new MyCollable(200));
//使用V get() 方法获取计算结果
Integer i1 = f1.get();
Integer i2 = f2.get();
//结束线程池
pool.shutdown();
}
}
15.多线程的匿名内部类实现方式
public class ThreadDemo{
public static void main(String[] args){
//基础Thread类实现多线程(使用子类对象)
new Thread(){
public void run(){
for(int x = 0; x < 100; x++){
System.our.println(Tread.currentThread().getName() + ":" +x)
}
}
}.start();
//实现Runnable接口来实现多线程(使用匿名内部类)
new Thred(new Runnable(){
public void run(){
for(int x = 0; x < 100; x++){
System.out.println(Thread.currentThread().getName() + ":" +x);
}
}
A:常用功能
punlic Timer():创建定时器对象的构造方法
public void schedule(TimerTask task,long dalay):指定多长时间后执行任务
public void schedule(TimerTask task,long delay,long period):指定多长时间后执行任务,指定多长时间后再次执行任务
public void cancel():取消计时器任务
B:案例演示
public class TimerDemo{
public static void main(String[] args){
//创建定时器对象
Timer t = new Timer();
//执行任务
t.schedule(new MyTask(),3000,2000);
//结束任务
t.cancel()
}
}
public class MyTask extends TimerTask{
public void run(){
System.out.println("任务已执行!");
}
}
C:在指定的时间内删除指定的目录
public class TimerTest{
public static void main(String[] args) throws ParseException {
Timer t = new Timer();
String str = "2015-12-12 23:59:59";
SimpleDeteFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(s);
t.schedule(new DeleteFolder(),d);
}
}
public class DeleteFolder extends TimerTask{
public void run(){
File srcFolder = new File("demo");
deleteFolder(srcForder);
}
//递归删除目录
public void deleteFolder(File srcFolder){
File[] fileArray = srcFolder.listFiles();
if(fileArray != null){
for(File file : fileArray){
if(file.isDirectory()){
deleteFolder(file);
}else{
System.out.println(file.getName()+ ":" +file.delete());
}
}
System.out.println(srcFolder.getName()+ ":" +srcFolder.delete());
}
}
}