Java学习笔记_15

来源:互联网 发布:中国人民航空大学知乎 编辑:程序博客网 时间:2024/06/07 16:43

一、线程中的一些方法

1.1 线程加入
  public final void join()
  等待该线程中止,其他线程才能继续抢着执行
1.2 线程礼让
  public static void yield():暂停当前正在执行的线程对象,并执行其他线程。 
  作用:让线程间的执行更和谐一些,但是实际上做不到。可以通过后面讲解的知识来实现。
1.3 线程死亡
   public final void stop():直接杀死(已过时)
   public void interrupt():直接杀死,在死前,还可以有遗言。

案例演示:

public class MyThread extends Thread{@Overridepublic void run() {System.out.println("开始时间"+new SimpleDateFormat("HH:mm:ss").format(new Date()));//睡个9秒try {Thread.sleep(9000);} catch (InterruptedException e) {// TODO Auto-generated catch blockSystem.out.println("我被杀了");}System.out.println("结束时间"+new SimpleDateFormat("HH:mm:ss").format(new Date()));}}
public class Text {public static void main(String[] args) {MyThread mt = new MyThread();mt.start();try {mt.sleep(3000);//mt.stop();mt.interrupt();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
如果这里调用的是stop方法,则线程在休眠三秒后被杀死。
如果是调用interrupt方法的话,则还会输出catch代码块中的内容,和打印结束时间

   
1.4 线程休眠
   static void sleep(long millis) 线程睡一会

二:线程的生命周期
1.新建
2.就绪
3.运行
4.有可能阻塞
5.死亡


三:线程间通信(生产消费者问题):不同类型线程针对同一个资源的操作

例如:
1.系统不仅要卖票还要入票
2.不仅要卖肉夹馍还要生产肉夹馍

案例:以给学生设置和获取姓名和年龄为例,演示线程通信问题
 线程间通讯:
资源:Student
设置数据线程:SetThread
获取数据线程:GetThread
测试类:StudentDemo
  
案例分析:

  问题1:
  打印null--0
  原因:设置和获取不是同一个对象
  请问,怎么解决?
  当在不同的类中,需要使用同一个数据的时候,可以考虑将数据利用构造方法进行传参
即是在SetThread和GetThread两个类中,创建SetThread和GetThread两个构造方法,传入参数Student s ,再令this.s = s;

在测试类中创建Student对象,传入这两个构造方法中,就能保证设置和获取的是同一个对象;

具体代码:

private Student s;public GetThread(Student s){this.s = s;}
private Student s;public SetThread(Student s){this.s = s;}

问题2:

2.1出现重复数据

2.2出现数据错乱,线程安全问题
        刘嘉玲--35
陈冠希--50

解决方法:加锁,加同一把锁(Student s);


问题3:

我们现在解决了数据的安全问题,但是又产生了一个新的问题,数据一打印就是一大片,
我们现在想让设置和获取的时候这两个线程之间的运行更加的和谐一些。

解决方法:通过线程的等待和唤醒完成需求

具体步骤:

1.在Student类中创建一个 bealoon flag;默认为false;

2.在SetThread类中,在写入数据前,判断Student对象中是否有数据,若flag为true,则让线程等待,否则正常录入数据,并将flag设置为true,唤醒线程;

3.在GetThread类中,在读取数据前,判断Student对象中是否有数据,若flag为false,则让线程等待,否则正常读取数据,并将flag设置为false,唤醒线程;

4.在StudentDemo类中调用方法,启动线程。

具体代码(利用同步代码块加锁):

Student类

public class Student {String name;int age;boolean falg;}

SetThread类

public class SetThread implements Runnable{private Student s;private int x = 0;public SetThread(Student s){this.s = s;}@Overridepublic void run() {while (true) {synchronized (s) {//有数据的话,等待if (s.falg) {try {s.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (x%2==0) {s.name="Eason";s.age = 40;}else {s.name = "Hins";s.age = 35;}x++;s.falg = true;s.notify();}}}}

GetThread类

public class GetThread implements Runnable{private Student s ;public GetThread(Student s){this.s = s;}@Overridepublic void run() {while (true) {synchronized (s) {if (!s.falg) {try {s.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(s.name+"--"+s.age);s.falg = false;s.notify();}}}}
测试类

public class StudentDemo {public static void main(String[] args) {Student 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();}}
输出结果是轮流输出

Eason--40
Hins--35


四:将上述代码继续优化
1.私有化Student类的成员变量
2.在类的内部提供设置和获取的同步方法

具体思路:

1.私有化Student类的成员变量,在Student类中加入同步方法setInfo(String name, int age)和getInfo();

2.同样创建SetThread类和GetThread类,在里面添加构造方法,保证Student s 是同一个对象;

3.在SetThread类和GetThread类中分别调用这两个方法;

4.创建测试类,调用方法测试;


具体代码:

Student类:

public class Student {private String name;private int age;private boolean flag;public synchronized void setInfo(String name,int age){if (this.flag) {try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//没有数据的话,设置数据this.name = name;this.age = age;this.flag = true;//唤醒线程this.notify();}public synchronized void getInfo(){if(!this.flag){try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//如果有数据的话System.out.println(this.name +"--"+this.age);this.flag = false;//唤醒线程this.notify();}}

SetThread类

public class SetThread implements Runnable{private Student s ;private int x = 0;public SetThread(Student s){this.s = s;}@Overridepublic void run() {// TODO Auto-generated method stubwhile (true) {if(x%2==0){s.setInfo("Tony", 11);}else{s.setInfo("Rocket", 12);}x++;}}

GetThread类

public class GetThread implements Runnable{private Student s;public GetThread(Student s){this.s = s;}public void run() {while (true) {s.getInfo();}}}
测试类

public class StudentDemo {public static void main(String[] args) {Student 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();}}
输出结果:两个学生的信息轮流输出;


五:线程组
  线程组:Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制。
  
  默认情况下,所有的线程都属于主线程组。
  public final ThreadGroup getThreadGroup():获取线程对应的线程组对象
  
  我们也可以给线程设置分组
  Thread(ThreadGroup group, Runnable target) 

  案例1:创建线程获取对应的线程组对象,并获取名称
先创建MyThread类和MyRunnable类,

public class Test {public static void main(String[] args) {MyThread mt1 = new MyThread();MyThread mt2 = new MyThread();ThreadGroup tp1 = mt1.getThreadGroup();ThreadGroup tp2 = mt2.getThreadGroup();System.out.println(tp1.getName());System.out.println(tp2.getName());ThreadGroup tp = new ThreadGroup("lau dehua");System.out.println(tp.getName());Thread t1 = new Thread(tp, new MyRunnable());Thread t2 = new Thread(tp, new MyRunnable());ThreadGroup tg3 = t1.getThreadGroup();ThreadGroup tg4 = t2.getThreadGroup();System.out.println(tg3.getName());System.out.println(tg4.getName());}}
这里输出结果为:

main

main

lau dehua

lau dehua

lau dehua

说明默认情况下,所有的线程都属于主线程

六:线程池

为什么要使用线程池?
程序启动一个新线程成本是比较高的,因为它涉及到要与操作系统进行交互。而使用线程池可以很好的提高性能,
尤其是当程序中要创建大量生存期很短的线程时,更应该考虑使用线程池。

线程池的特点:
线程池里的每一个线程代码结束后,并不会死亡,而是再次回到线程池中成为空闲状态,等待下一个对象来使用。
在JDK5之前,我们必须手动实现自己的线程池,从JDK5开始,Java内置支持线程池

线程池如何创建?
JDK5新增了一个Executors工厂类来产生线程池,有如下几个方法
public static ExecutorService newFixedThreadPool(int nThreads)

线程池的使用步骤:
1.创建线程池对象
ExecutorService pool = Executors.newFixedThreadPool(2);

2.创建Runnable实例
MyRunnable my = new MyRunnable();

3.提交Runnable实例
pool.submit(my);
pool.submit(my);

4.关闭线程池
pool.shutdown();

案例3:实现Callable接口实现线程池的使用,实现多线程求和,1-10之和,1-100之和

Callable接口也是一个任务只不过这个任务需要执行的方法是call(),这个方法有返回值;

public class MyCallable implements Callable<Integer>{//这里面的泛型就是call()方法返回值的类型private int start;private int end;public MyCallable(int start,int end){this.start = start;this.end = end;}@Overridepublic Integer call() throws Exception {//在这里求取start--end之间的和int sum = 0;for (int i = start; i < end+1; i++) {sum+=i;}return sum;}}
public class Test {public static void main(String[] args) throws Exception{//1-10之和,1-100之和//1.创建线程池ExecutorService pool = Executors.newFixedThreadPool(2);//2.创建任务对象,创建任务对象的同时,将参数进行传递MyCallable my1 = new MyCallable(1,10);MyCallable my2 = new MyCallable(1, 100);MyCallable my3 = new MyCallable(1, 1000);//3.提交任务<T> Future<T> submit(Callable<T> task)Future<Integer> res = pool.submit(my1);Future<Integer> res2 = pool.submit(my2);Future<Integer> res3 = pool.submit(my3);//V get()如有必要,等待计算完成,然后获取其结果。 System.out.println(res.get());System.out.println(res2.get());System.out.println(res3.get());//4.关闭线程池pool.shutdown();}}

七:定时器

Timer
public Timer()构造
public void schedule(TimerTask task, long delay)延迟多久执行任务
public void schedule(TimerTask task,long delay,long period)延迟多久执行任务,并以后每隔多久执行一次
public boolean cancel()取消这个任务


TimerTask
public abstract void run()放的是所要执行的任务代码                                                                                                                                            
案例:定时删除某个文件

public class Test {public static void main(String[] args) throws Exception {/** * 案例2:定时删除文件(需要在15:58:00 删除D://a.txt文件) * 所用方法:schedule(TimerTask task, Date time) 安排在指定的时间执行指定的任务 * 时间点:16:30:00 * 任务:删除D://a.txt文件 *///创建定时器Timer t = new Timer();String time = "2017-05-20 16:31:00";Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);//所用方法:schedule(TimerTask task, Date time) 安排在指定的时间执行指定的任务t.schedule(new MyTimerTask(), date);}}class MyTimerTask extends TimerTask{@Overridepublic void run() {//任务:删除D://a.txt文件File file = new File("D://a.txt");file.delete();}}








原创粉丝点击