多线程(下)

来源:互联网 发布:数据etl工具 编辑:程序博客网 时间:2024/06/04 00:22

一:线程中的一些方法
1.1 线程加入
public final void join()
等待该线程中止,其他线程才能继续抢着执行
因为线程执行的时候,每个线程都会抢占CPU的执行权,所以我们可以利用此方法先让一个线程执行完毕后,然后再去执行其他的线程

package com.edu_01;public class MyThread extends Thread{    @Override    public void run() {        for (int i = 0; i < 100; i++) {            System.out.println(getName()+"--"+i);        }    }}public class Test {    public static void main(String[] args) {        //创建三个线程        MyThread mt1 = new MyThread();        MyThread mt2 = new MyThread();        MyThread mt3 = new MyThread();        //给线程起名字        mt1.setName("刘备");        mt2.setName("曹操");        mt3.setName("孙权");        //开启三个线程        mt1.start();        //接着让mt1这个线程设置为加入线程,其他线程就没有抢占cpu执行权的权利了,只能等待该线程执行完毕之后,才能开始抢占        try {            mt1.join();        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        mt2.start();        mt3.start();    }}

1.2 线程礼让
public static void yield():暂停当前正在执行的线程对象,并执行其他线程。
作用:让线程间的执行更和谐一些,也就是让线程抢占CPU的概率相同一点

package com.edu_02;public class MyThread extends Thread{    @Override    public void run() {        for (int i = 0; i < 100; i++) {            System.out.println(getName()+"---"+i);            //实现县城礼让            Thread.yield();        }    }}public class Test {    public static void main(String[] args) {        //创建两个线程        MyThread mt1 = new MyThread();        MyThread mt2 = new MyThread();        //给线程设置名字        mt1.setName("郭德纲");        mt2.setName("周立波");        //开启线程        mt1.start();        mt2.start();    }}

1.3 线程死亡
public final void stop():直接杀死(即终止当前运行的线程,线程不会往下执行)
public void interrupt():直接杀死,在死前,还可以有遗言。(也就是说,线程还可以将该程序执行完)

package com.edu_03;import java.text.SimpleDateFormat;import java.util.Date;public class MyThread extends Thread{    @Override    public void run() {        //打印一下开始执行的时间        System.out.println("开始时间:"+new SimpleDateFormat("HH:mm:ss").format(new Date()));        //休眠10秒钟        try {            Thread.sleep(10000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            //e.printStackTrace();            System.out.println("我被杀死了");        }        System.out.println("结束时间:"+new SimpleDateFormat("HH:mm:ss").format(new Date()));    }}public class Test {    public static void main(String[] args) {        //创建线程对象        MyThread mt = new MyThread();        //开启线程对象        mt.start();        //在线程处于睡眠的过程中将他杀死        try {            Thread.sleep(3000);            //杀死刚刚开启的线程            //调用stop()方法将线程直接杀死            //mt.stop();//划了一条横线表示该方法已经过时,但是还可以使用            //interrupt():直接杀死,在死前,还可以有遗言。            mt.interrupt();//线程被杀死之后会将后面的代码执行完毕之后,再死去        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

1.4 线程休眠
static void sleep(long millis) 线程睡一会
让线程休眠,一定会醒来的,

二:线程的生命周期
1.新建:线程的对象调用Start()方法
2.就绪:线程有cpu的执行权,但是没有CPU执行资格,开始抢占CPU的执行权
3.运行:抢占到了CPU的执行权,开始执行。(如果被其他线程抢到了执行权,则会恢复到就绪状态)
4.有可能阻塞:线程对象调用sleep方法,wait方法,在线程睡醒之后,或者被唤醒之后,就会恢复到就绪状态,
5.死亡:线程调用stop方法,或者调用interrupt方法,或者在线程执行完run方法后,
这里写图片描述

三:线程间通信(生产消费者问题):不同类型线程针对同一个资源的操作
1.系统不仅要卖票还要入票
每次生产一张票,就会卖出去一张票,称为单生产单消费问题
2.不仅要卖肉夹馍还要生产肉夹馍
可以利用多个线程去生产肉夹馍,然后利用多个线程去出售
称为多生产,多消费问题

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

package com.edu_04;public class Student {    String name;    int age;}public class SetThread implements Runnable{    private Student s;    private int x=0;    public SetThread(Student s){        this.s = s;    }    @Override    public void run() {        //给学生对象设置姓名和年龄        //Student s = new Student();        while (true) {            synchronized (s) {                if (x%2==0) {                    s.name = "刘嘉玲";                    s.age = 50;                }else {                    s.name = "陈冠希";                    s.age = 35;                }                x++;            }        }    }}public class GetThread implements Runnable{    private Student s;    public GetThread(Student s){        this.s = s;    }    @Override    public void run() {        //获取线程,获取学生对象的姓名和年龄        //Student s = new Student();        while (true) {            synchronized (s) {                  System.out.println(s.name+"--"+s.age);              }        }    }}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();    }}

问题1:控制台出现的结果是:null—0
因为我们在在创建对象的时候,setThread()和getThread()的对象不是同一个,所以在输出的时候,因为setThread()还没有设置对象,所以我在传递参数的时候可以自己创建构造方法,让这个参数一致
设置和获取线程使用的学生资源不是同一个,把资源作为构造参数传递即可。

问题2:
A:相同的数据出现了多次
CPU的一点点时间就足够我们的程序执行很多次
B:数据出现了问题(数据安全问题)
a:是否是多线程环境

b:是否有共享数据

c:是否有多条语句操作共享数据

既然我们知道它是出现了数据安全问题,我们就应该来解决它。
如何解决呢?加锁

问题3:加了锁以后,数据还是有问题
A:多个线程都要加锁
B:多个线程加的锁必须是同一把锁
因为,线程在执行的时候,每个线程的都会有抢占的随机性,所以输出的时候没有那么和谐,而且会出现错误的数据,出现线程不安全问题,所以我们要给线程加锁,保证线程数据安全,我们可以用等待,唤醒机制保证数据的和谐

五:将上述代码使用等待唤醒机制改进,实现礼让效果

package com.edu_05;public class Student {    String name;    int age;    boolean flag;//在这里可以作为对象的一个标记,如果是false说明该对象没有数据,如果是true说明该对象有数据}public class SetThread implements Runnable{    private Student s;    private int x = 0;    public SetThread(Student s){        this.s = s;    }    @Override    public void run() {        while (true) {            synchronized (s) {                //判断该对象此时有没有数据                if (s.flag) {                    //等待                    try {                        s.wait();//设置线程等待,释放锁s                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }                if (x%2==0) {                    s.name = "刘嘉玲";                    s.age = 50;                }else {                    s.name = "冠希";                    s.age = 35;                }                x++;//x=1                //此时对象有数据了                s.flag = true;                s.notify();//如果有等待的线程就唤醒,如果没有等待的线程,则没有任何效果            }//在此时释放锁对象s        }    }}public class GetThread implements Runnable{    private Student s;    public GetThread(Student s){        this.s = s;    }    @Override    public void run() {        while (true) {            synchronized (s) {                //判断对象有没有数据                if (!s.flag) {                    //等待设置线程给对象设置数据                    try {                        s.wait();//获取线程处于等待状态,释放锁对象s,在哪里跌倒在哪里爬起来                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }                System.out.println(s.name+"--"+s.age);//刘嘉玲--50                                                      //冠希--35                                                      //刘嘉玲--50                //当获取线程从学生对象中获取了数据之后,我们就默认他已经没有数据了,此时我们应该                //继续让设置线程继续给学生对象设置信息                s.flag = 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();    }}

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

public class Student {    //块编辑(alt+shift+a):在使用块编辑的时候,一定要将输入法切换到英文输入法,不然会出问题    private String name;    private int age;    private boolean flag;//在这里可以作为对象的一个标记,如果是false说明该对象没有数据,如果是true说明该对象有数据    //提供公共的方法设置信息    public synchronized void setInfo(String name,int age){        if (this.flag) {            //等待            try {                this.wait();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.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 block                e.printStackTrace();            }        }        //有数据,取数据        System.out.println(this.name+"--"+this.age);        //取完数据之后,就没有数据了        this.flag = false;        this.notify();    }}public class SetThread implements Runnable{    private Student s;    private int x = 0;    public SetThread(Student s){        this.s = s;    }    @Override    public void run() {        while (true) {                if (x%2==0) {                    s.setInfo("刘嘉玲", 50);                }else {                    s.setInfo("陈冠希", 35);                }                x++;//x=1            }    }}public class GetThread implements Runnable{    private Student s;    public GetThread(Student s){        this.s = s;    }    @Override    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:创建线程获取对应的线程组对象,并获取名称
案例2:创建线程组对象,给线程分配线程组

public class MyRunnable implements Runnable{    @Override    public void run() {    }}public class MyThread extends Thread{    @Override    public void run() {    }}public class Test {    public static void main(String[] args) {        // 案例1:创建线程获取对应的线程组对象,并获取名称        //创建两个线程        MyThread mt1 = new MyThread();        MyThread mt2 = new MyThread();        //获取上面两个线程对应的线程组对象        ThreadGroup tg1 = mt1.getThreadGroup();        ThreadGroup tg2 = mt2.getThreadGroup();        //获取线程组对象的名称        System.out.println(tg1.getName());//main--主线程祖,默认情况下,每一个线程默认都属于主线程祖        System.out.println(tg2.getName());        System.out.println("-----------------");        //案例2:创建线程组对象,给线程分配线程组        //public ThreadGroup(String name)        ThreadGroup tg = new ThreadGroup("刘德华");        //public Thread(ThreadGroup group,Runnable target)        Thread t3 = new Thread(tg, new MyRunnable());        Thread t4 = new Thread(tg, new MyRunnable());        //获取t3和t4的线程组对象        ThreadGroup tg3 = t3.getThreadGroup();        ThreadGroup tg4 = t4.getThreadGroup();        System.out.println(tg3.getName());//刘德华        System.out.println(tg4.getName());//刘德华    }}

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

线程池的特点:
线程池里的每一个线程代码结束后,并不会死亡,而是再次回到线程池中成为空闲状态,等待下一个对象来使用。
在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();

案例1:实现Runnable接口实现线程池的使用

package com.edu_08;public class MyRunnbale implements Runnable {    @Override    public void run() {        for (int i = 0; i < 100; i++) {            System.out.println(Thread.currentThread().getName()+"---"+i);        }    }}public class ThreadPool {    public static void main(String[] args) {        //线程池如何创建?        //1.调用工厂类Executors        //的public static ExecutorService newFixedThreadPool(int nThreads),返回一个线程池对象        ExecutorService pool = Executors.newFixedThreadPool(2);        //2.提交给线程池两个任务,都是打印0-99        //创建任务        MyRunnbale my1 = new MyRunnbale();        MyRunnbale my2 = new MyRunnbale();        //3.提交任务        pool.submit(my1);        pool.submit(my2);        //关闭线程池        //void shutdown()        pool.shutdown();    }}

案例2:实现Callable接口实现线程池的使用

package com.edu_09;import java.util.concurrent.Callable;public class MyCallable implements Callable{    //也是一个任务,只不过这个任务需要执行的方法是call(),这个方法有返回值    @Override    public Object call() throws Exception {        for (int i = 0; i < 100; i++) {            System.out.println(Thread.currentThread().getName()+"---"+i);        }        return null;    }}import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPool {    public static void main(String[] args) {        //案例2:实现Callable接口实现线程池的使用        //1.创建线程池        ExecutorService pool = Executors.newFixedThreadPool(2);        //创建一个任务        MyCallable my1 = new MyCallable();        MyCallable my2 = new MyCallable();        //3.提交任务        pool.submit(my1);        pool.submit(my2);        //4.关闭线程池        pool.shutdown();    }}

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

package com.edu_10;import java.util.concurrent.Callable;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;    }    @Override    public Integer call() throws Exception {        //在这里求取start--end之间的和        int sum = 0;        for (int i = start; i < end+1; i++) {            sum+=i;        }        return sum;    }}package com.edu_10;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;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()放的是所要执行的任务代码

案例1:如何利用上述方法:

package com.edu_11;import java.util.Timer;import java.util.TimerTask;public class TimerTest {    public static void main(String[] args) {        //需求:在10秒钟后,在控制台打印一句话,helloworld        //public Timer()构造        Timer t = new Timer();        //public void schedule(TimerTask task, long delay)延迟多久执行任务        t.schedule(new MyTimerTask(t), 10000);        //public void cancel()终止此计时器        //t.cancel();//如果在这里关闭的话,我们还没等任务执行完毕呢,计时器已经被关闭了    }}//创建TimerTask的子类class MyTimerTask extends TimerTask{    private Timer t;    public MyTimerTask(Timer t){        this.t = t;    }    @Override    public void run() {        //此计时器任务要执行的操作。         System.out.println("helloworld");        t.cancel();//当任务被执行完毕之后,关闭定时器    }}

案例2:定时删除文件
所用方法:schedule(TimerTask task, Date time) 安排在指定的时间执行指定的任务

package com.edu_12;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Timer;import java.util.TimerTask;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{    @Override    public void run() {        //任务:删除D://a.txt文件        File file = new File("D://a.txt");        file.delete();    }}

十:多线程相关题
1.多线程有几种实现方案,分别是哪几种?
有俩种实现方案:1.实现Runnable()接口
2.继承Thread()类
2.同步有几种方式,分别是什么?
同步有三种方式:1.同步代码块
2.同步方法
3.静态同步方法
3.启动一个线程是run()还是start()?它们的区别?
启动线程利用start(),run()方法指的是对象调方法,在主线程里面执行该方法,start()指的是开启线程,在线程里面执行该方法
4.sleep()和wait()方法的区别
sleep():线程休眠,一定会醒来,而且线程在休眠的时候,不会放下自己手中的锁,
wait():线程等待,线程等待的时候会放下自己手中的锁,线程等待需要唤醒,不唤醒,不执行
5.为什么wait(),notify(),notifyAll()等方法都定义在Object类中
这些方法存在于同步中,使用这些方法的时候,必须表明所属的锁,锁可以是任意对象,但是必须保证是同一把锁,所以这些方法都定义在Object()中

原创粉丝点击