java-多线程

文章目录

多线程

一.先看个思维导图呗

在这里插入图片描述

二.启动一个线程

多线程即在同一时间,可以做多件事情。
创建多线程有3种方式,分别是继承线程类,实现Runnable接口,匿名类

1.线程概念

首先要理解进程(Processor)和线程(Thread)的区别
进程:启动一个LOL.exe就叫一个进程。 接着又启动一个DOTA.exe,这叫两个进程。
线程:线程是在进程内部同时做的事情,比如在LOL里,有很多事情要同时做,比如"盖伦” 击杀“提莫”,同时“赏金猎人”又在击杀“盲僧”,这就是由多线程来实现的。

以下代码演示的是不使用多线程的情况
只有在盖伦杀掉提莫后,赏金猎人才开始杀盲僧。

package multiplethread;
import java.io.Serializable;
public class Hero{
    public String name;
    public float hp;
    public int damage;
    public void attackHero(Hero h) {
        try {
            //为了表示攻击需要时间,每次攻击暂停1000毫秒
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
         
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
 
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;
import multiplethread.Hero;
public class TestThread {
    public static void main(String[] args) {  
        Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 616;
        gareen.damage = 50;
 
        Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 300;
        teemo.damage = 30;
         
        Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 500;
        bh.damage = 65;
         
        Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 455;
        leesin.damage = 80;
         
        //盖伦攻击提莫
        while(!teemo.isDead()){
            gareen.attackHero(teemo);
        }
 
        //赏金猎人攻击盲僧
        while(!leesin.isDead()){
            bh.attackHero(leesin);
        }
    } 
}

在这里插入图片描述

2.创建多线程的三种方式

2.1 创建多线程-继承线程类

使用多线程,就可以做到盖伦在攻击提莫的同时,赏金猎人也在攻击盲僧
设计一个类KillThread 继承Thread,并且重写run方法
启动线程办法: 实例化一个KillThread对象,并且调用其start方法
就可以观察到 赏金猎人攻击盲僧的同时,盖伦也在攻击提莫

package multiplethread;
import multiplethread.Hero;
public class KillThread extends Thread{
    private Hero h1;
    private Hero h2;
 
    public KillThread(Hero h1, Hero h2){
        this.h1 = h1;
        this.h2 = h2;
    }
 
    public void run(){
        while(!h2.isDead()){
            h1.attackHero(h2);
        }
    }
}
package multiplethread;
import multiplethread.Hero;
public class TestThread {
    public static void main(String[] args) {  
        Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 616;
        gareen.damage = 50;
 
        Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 300;
        teemo.damage = 30;
         
        Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 500;
        bh.damage = 65;
         
        Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 455;
        leesin.damage = 80;
         
        KillThread killThread1 = new KillThread(gareen,teemo);
        killThread1.start();
        KillThread killThread2 = new KillThread(bh,leesin);
        killThread2.start();     
    }  
}

在这里插入图片描述

2.2 创建多线程-实现Runnable接口

创建类Battle,实现Runnable接口
启动的时候,首先创建一个Battle对象,然后再根据该battle对象创建一个线程对象,并启动

Battle battle1 = new Battle(gareen,teemo);
new Thread(battle1).start();

battle1 对象实现了Runnable接口,所以有run方法,但是直接调用run方法,并不会启动一个新的线程。
必须,借助一个线程对象的start()方法,才会启动一个新的线程。
所以,在创建Thread对象的时候,把battle1作为构造方法的参数传递进去,这个线程启动的时候,就会去执行battle1.run()方法了。

package multiplethread;
import multiplethread.Hero;
public class Battle implements Runnable{ 
    private Hero h1;
    private Hero h2;
 
    public Battle(Hero h1, Hero h2){
        this.h1 = h1;
        this.h2 = h2;
    }
    public void run(){
        while(!h2.isDead()){
            h1.attackHero(h2);
        }
    }
}
package multiplethread;
import multiplethread.Hero;
public class TestThread {
    public static void main(String[] args) {  
        Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 616;
        gareen.damage = 50;
 
        Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 300;
        teemo.damage = 30;
         
        Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 500;
        bh.damage = 65;
         
        Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 455;
        leesin.damage = 80;
         
        Battle battle1 = new Battle(gareen,teemo);
        
        //new Thread(Runnable接口实现类对象):创建子线程
        //将实现类对象以参数的形式传递到Thread对象的创建过程中
        new Thread(battle1).start();
 
        Battle battle2 = new Battle(bh,leesin);
        new Thread(battle2).start();
    } 
}

在这里插入图片描述

2.3 创建多线程-匿名类

使用匿名类,继承Thread,重写run方法,直接在run方法中写业务代码
匿名类的一个好处是可以很方便的访问外部的局部变量。
前提是外部的局部变量需要被声明为final。(JDK7以后就不需要了)

package multiplethread;
import multiplethread.Hero;
public class TestThread {
    public static void main(String[] args) {  
        Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 616;
        gareen.damage = 50;
 
        Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 300;
        teemo.damage = 30;
         
        Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 500;
        bh.damage = 65;
         
        Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 455;
        leesin.damage = 80;
        //匿名类
        Thread t1= new Thread(){
            public void run(){
                //匿名类中用到外部的局部变量teemo,必须把teemo声明为final
                //但是在JDK7以后,就不是必须加final的了
                while(!teemo.isDead()){
                    gareen.attackHero(teemo);
                }              
            }
        };
        t1.start();
          
        Thread t2= new Thread(){
            public void run(){
                while(!leesin.isDead()){
                    bh.attackHero(leesin);
                }              
            }
        };
        t2.start();
    } 
}

在这里插入图片描述

2.4 创建多线程的三种方式(总)

把上述3种方式再整理一下:

  1. 继承Thread类
  2. 实现Runnable接口
  3. 匿名类的方式

注: 启动线程是start()方法,run()并不能启动一个新的线程

2.5 练习-同步查找文件内容

把 练习-查找文件内容 改为多线程查找文件内容
原练习的思路是遍历所有文件,当遍历到文件是 .java的时候,查找这个文件的内容,查找完毕之后,再遍历下一个文件

现在通过多线程调整这个思路:
遍历所有文件,当遍历到文件是.java的时候,创建一个线程去查找这个文件的内容,不必等待这个线程结束,继续遍历下一个文件。

package multiplethread;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class SearchContent {
	public static void main(String[] args) {
		File f = new File("D:\\LOLFolder");
		File[] dir= f.listFiles();
		Scanner scanner=new Scanner(System.in);
		System.out.println("输入你想查找的内容:");
		String search=scanner.nextLine();
		System.out.printf("\n内容包括%s的那些文件:%n",search);
		search(dir,search);
	}
	public static void search(File dir[], String search){
		if (dir == null) {
			return;
		} else {
			for (File fi : dir) {
				if (fi.isDirectory()) {
					search(fi.listFiles(), search);
				}
				if (fi.isFile()&&fi.getName().endsWith(".java")) {
					//匿名类
					Thread t1 = new Thread() {
                        public void run() {
                            System.out.println("开启了一个线程!!~!");
							try(BufferedReader br = new BufferedReader(new FileReader(fi));){
								while(true){
				                    String line=br.readLine();
				                    if(line==null){
				                        break;
				                    }
				                    if(line.contains(search)){
				                        System.out.printf("文件:%s%n",fi.getAbsolutePath());
				                        break;
				                    }
				                }
							} catch (FileNotFoundException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}  
                        }
                    };
                    t1.start();
				}
			}
		}	
	}
}

在这里插入图片描述

三.常见线程方法

1.sleep 当前线程暂停

package multiplethread;
public class TestThread {
    public static void main(String[] args) {  
        Thread t1= new Thread(){
            public void run(){
                int seconds =0;
                while(true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.printf("已经玩了LOL %d 秒%n", seconds++);
                }              
            }
        };
        t1.start();       
    }  
}

在这里插入图片描述

2.join 加入到当前线程中

所有进程,至少会有一个线程即主线程,即main方法开始执行,就会有一个看不见的主线程存在。
在42行执行t.join,即表明在主线程中加入该线程。
主线程会等待该线程结束完毕, 才会往下运行。

package multiplethread;
import multiplethread.Hero;
public class TestThread {
    public static void main(String[] args) {    
        final Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 616;
        gareen.damage = 50;
  
        final Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 300;
        teemo.damage = 30;
          
        final Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 500;
        bh.damage = 65;
          
        final Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 455;
        leesin.damage = 80;
          
        Thread t1= new Thread(){
            public void run(){
                while(!teemo.isDead()){
                    gareen.attackHero(teemo);
                }              
            }
        };
          
        t1.start();
 
        //代码执行到这里,一直是main线程在运行
        try {
            //t1线程加入到main线程中来,只有t1线程运行结束,才会继续往下走
            t1.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        Thread t2= new Thread(){
            public void run(){
                while(!leesin.isDead()){
                    bh.attackHero(leesin);
                }              
            }
        };
        //会观察到盖伦把提莫杀掉后,才运行t2线程
        t2.start();      
    } 
}

在这里插入图片描述

3.setPriority 线程优先级

当线程处于竞争关系的时候,优先级高的线程会有更大的几率获得CPU资源
为了演示该效果,要把暂停时间去掉,多条线程各自会尽力去占有CPU资源
同时把英雄的血量增加100倍,攻击减低到1,才有足够的时间观察到优先级的演示
如图可见,线程1的优先级是MAX_PRIORITY,所以它争取到了更多的CPU资源执行代码

package multiplethread;
import java.io.Serializable; 
public class Hero{
    public String name;
    public float hp;
      
    public int damage;
      
    public void attackHero(Hero h) {
        //把暂停时间去掉,多条线程各自会尽力去占有CPU资源
        //线程的优先级效果才可以看得出来
//        try {
//           
//            Thread.sleep(0);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
          
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    } 
}
package multiplethread;  
import multiplethread.Hero;
public class TestThread {
    public static void main(String[] args) {  
        final Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 6160;
        gareen.damage = 1;
  
        final Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 3000;
        teemo.damage = 1;
          
        final Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 5000;
        bh.damage = 1;
          
        final Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 4505;
        leesin.damage = 1;
          
        Thread t1= new Thread(){
            public void run(){
                while(!teemo.isDead()){
                    gareen.attackHero(teemo);
                }              
            }
        };
          
        Thread t2= new Thread(){
            public void run(){
                while(!leesin.isDead()){
                    bh.attackHero(leesin);
                }              
            }
        };
         
        t1.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);
        t1.start();
        t2.start();       
    }    
}

在这里插入图片描述

4.yield 临时暂停

当前线程,临时暂停,使得其他线程可以有更多的机会占用CPU资源。

package multiplethread;  
import multiplethread.Hero; 
public class TestThread {
    public static void main(String[] args) { 
        final Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 61600;
        gareen.damage = 1;
  
        final Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 30000;
        teemo.damage = 1;
          
        final Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 50000;
        bh.damage = 1;
          
        final Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 45050;
        leesin.damage = 1;
          
        Thread t1= new Thread(){
            public void run(){
                while(!teemo.isDead()){
                    gareen.attackHero(teemo);
                }              
            }
        };
          
        Thread t2= new Thread(){
            public void run(){
                while(!leesin.isDead()){
                    //临时暂停,使得t1可以占用CPU资源
                    Thread.yield();    
                    bh.attackHero(leesin);
                }              
            }
        };
         
        t1.setPriority(5);
        t2.setPriority(5);
        t1.start();
        t2.start();      
    }      
}

在这里插入图片描述

5.setDaemon 守护线程

守护线程的概念是: 当一个进程里,所有的线程都是守护线程的时候,结束当前进程。
就好像一个公司有销售部,生产部这些和业务挂钩的部门。
除此之外,还有后勤,行政等这些支持部门。
如果一家公司销售部,生产部都解散了,那么只剩下后勤和行政,那么这家公司也可以解散了。
守护线程就相当于那些支持部门,如果一个进程只剩下守护线程,那么进程就会自动结束。
守护线程通常会被用来做日志,性能统计等工作。

package multiplethread;
public class TestThread {
    public static void main(String[] args) { 
        Thread t1= new Thread(){
            public void run(){
                int seconds =0;
                 
                while(true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.printf("已经玩了LOL %d 秒%n", seconds++);
                     
                }              
            }
        };
        t1.setDaemon(true);//守护线程,那么进程就会自动结束。
        t1.start();      
    }  
}

6.练习

英雄充能

英雄有可以放一个技能叫做: 波动拳。
每隔一秒钟,可以发一次,但是只能连续发3次。
发完3次之后,需要充能5秒钟,充满,再继续发。

package multiplethread;
public class TestThread {
    public static void main(String[] args) {     
        Thread t1= new Thread(){
            public void run(){
                int seconds =1;    
                while(true){
                    try {
                    	if(seconds<=3){
                    		Thread.sleep(1000);
                    	}else{
                    		seconds=1;
                    		System.out.println("开始为时5秒的充能");
                    		Thread.sleep(5000);		
                    	}       
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.printf("波动拳第%d发%n", seconds++);      
                }              
            }
        };
        t1.start();        
    }    
}

在这里插入图片描述

破解密码

  1. 生成一个长度是3的随机字符串,把这个字符串当作 密码
  2. 创建一个破解线程,使用穷举法,匹配这个密码
  3. 创建一个日志线程,打印都用过哪些字符串去匹配,这个日志线程设计为守护线程

提示: 破解线程把穷举法生成的可能密码放在一个容器中,日志线程不断的从这个容器中拿出可能密码,并打印出来。 如果发现容器是空的,就休息1秒,如果发现不是空的,就不停的取出,并打印。

package multiplethread;

import java.util.ArrayList;
import java.util.List;

public class TestString { 
    public static void main(String[] args) {
    	StringBuilder str1 = new StringBuilder();
    	String password;//存放长度是3的随机字符串
        StringBuilder str3= new StringBuilder(); 
        List<String> list=new ArrayList<>();
    	for (short i = '0'; i <= 'z'; i++) {
            if (Character.isLetter((char) i) || Character.isDigit((char) i)) {
                str1.append((char) i);
            }
        }
        String str2= str1.toString();
        
        for(int i=0;i<3;i++){
            int idx =(int) Math.floor(Math.random() * str2.length());
            str3.append(str2.charAt(idx));//str2.charAt(idx);得到一个字符
        }
        //长度是3的随机字符串
        password=str3.toString();
        System.out.println("生成的密码是:");
        System.out.println(str3);
        System.out.println("---------------");
        
        //创建一个破解线程,使用穷举法,匹配这个密码 
       Thread t1=new Thread(){
            public void run() {
                outloop:
                for(int i=0;i<str2.length();i++){
                	for(int j=0;j<str2.length();j++){
                		for(int k=0;k<str2.length();k++){
                			String check = ""+str2.charAt(i) + str2.charAt(j) + str2.charAt(k);
                			list.add(check);
                			if (check.equals(password)) {
                                System.out.println("密码是:" + check);
                                break outloop;
                			}
                		}
                    }
                }
            }
        };
        Thread t2=new Thread(){
            public void run(){
                while (true){
                    if (list.size()==0) {
                        try {
                        	//发现容器是空的,就休息1秒
                        	Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    else{
                    	//日志线程不断的从这个容器中拿出可能密码
                    	System.out.println(list.remove(0));
                    }                    
                }
            }
        };
        t2.setDaemon(true);
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
        
    }
}

在这里插入图片描述

在这里插入图片描述

四.同步

同步问题指的是多个线程同时修改一个数据的时候,可能导致的问题 。(多线程的问题,又叫Concurrency 问题)

1.演示同步问题

假设盖伦有10000滴血,并且在基地里,同时又被对方多个英雄攻击
就是有多个线程在减少盖伦的hp
同时又有多个线程在恢复盖伦的hp
假设线程的数量是一样的,并且每次改变的值都是1,那么所有线程结束后,盖伦应该还是10000滴血。
但是。。。
注意: 不是每一次运行都会看到错误的数据产生,多运行几次,或者增加运行的次数。

package multiplethread;
public class Hero{
    public String name;
    public float hp;
     
    public int damage;
     
    //回血
    public void recover(){
        hp=hp+1;
    }
     
    //掉血
    public void hurt(){
        hp=hp-1;
    }
     
    public void attackHero(Hero h) {
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;    
import multiplethread.Hero;    
public class TestThread {  
    public static void main(String[] args) {     
        final Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 10000;
           
        System.out.printf("盖伦的初始血量是 %.0f%n", gareen.hp);
           
        //多线程同步问题指的是多个线程同时修改一个数据的时候,导致的问题
           
        //假设盖伦有10000滴血,并且在基地里,同时又被对方多个英雄攻击
           
        //用JAVA代码来表示,就是有多个线程在减少盖伦的hp
        //同时又有多个线程在恢复盖伦的hp
           
        //n个线程增加盖伦的hp
           
        int n = 10000;
   
        Thread[] addThreads = new Thread[n]; //n个线程
        Thread[] reduceThreads = new Thread[n];
           
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                    gareen.recover();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            addThreads[i] = t;    
        }
           
        //n个线程减少盖伦的hp
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                    gareen.hurt();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            reduceThreads[i] = t;
        }
           
        //等待所有增加线程结束
        for (Thread t : addThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //等待所有减少线程结束
        for (Thread t : reduceThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
           
        //代码执行到这里,所有增加和减少线程都结束了
           
        //增加和减少线程的数量是一样的,每次都增加,减少1.
        //那么所有线程都结束后,盖伦的hp应该还是初始值
           
        //但是事实上观察到的是:
                   
        System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量变成了 %.0f%n", n,n,gareen.hp);     
    }     
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.分析同步问题产生的原因

  1. 假设增加线程先进入,得到的hp是10000
  2. 进行增加运算
  3. 正在做增加运算的时候,还没有来得及修改hp的值,减少线程来了
  4. 减少线程得到的hp的值也是10000
  5. 减少线程进行减少运算
  6. 增加线程运算结束,得到值10001,并把这个值赋予hp
  7. 减少线程也运算结束,得到值9999,并把这个值赋予hp
    hp,最后的值就是9999
    虽然经历了两个线程各自增减了一次,本来期望还是原值10000,但是却得到了一个9999
    这个时候的值9999是一个错误的值,在业务上又叫做脏数据

在这里插入图片描述

3.解决思路

总体解决思路是: 在增加线程访问hp期间,其他线程不可以访问hp

  1. 增加线程获取到hp的值,并进行运算
  2. 在运算期间,减少线程试图来获取hp的值,但是不被允许
  3. 增加线程运算结束,并成功修改hp的值为10001
  4. 减少线程,在增加线程做完后,才能访问hp的值,即10001
  5. 减少线程运算,并得到新的值10000

在这里插入图片描述

4.synchronized 同步对象概念

解决上述问题之前,先理解
synchronized关键字的意义
如下代码:

Object someObject =new Object();
synchronized (someObject){
  //此处的代码只有占有了someObject后才可以执行
}

synchronized表示当前线程,独占 对象 someObject
当前线程独占了对象someObject,如果有其他线程试图占有对象someObject,就会等待,直到当前线程释放对someObject的占用。
someObject 又叫同步对象,所有的对象,都可以作为同步对象
为了达到同步的效果,必须使用同一个同步对象

释放同步对象的方式: synchronized 块自然结束,或者有异常抛出

在这里插入图片描述

package multiplethread;  
import java.text.SimpleDateFormat;
import java.util.Date; 
public class TestThread {   
    public static String now(){
        return new SimpleDateFormat("HH:mm:ss").format(new Date());
    }
     
    public static void main(String[] args) {
        final Object someObject = new Object();
          
        Thread t1 = new Thread(){
            public void run(){
                try {
                    System.out.println( now()+" t1 线程已经运行");
                    System.out.println( now()+this.getName()+ " 试图占有对象:someObject");
                    synchronized (someObject) {
                          
                        System.out.println( now()+this.getName()+ " 占有对象:someObject");
                        Thread.sleep(5000);
                        System.out.println( now()+this.getName()+ " 释放对象:someObject");
                    }
                    System.out.println(now()+" t1 线程结束");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t1.setName(" t1");
        t1.start();
        
        Thread t2 = new Thread(){
            public void run(){
                try {
                    System.out.println( now()+" t2 线程已经运行");
                    System.out.println( now()+this.getName()+ " 试图占有对象:someObject");
                    synchronized (someObject) {
                        System.out.println( now()+this.getName()+ " 占有对象:someObject");
                        Thread.sleep(5000);
                        System.out.println( now()+this.getName()+ " 释放对象:someObject");
                    }
                    System.out.println(now()+" t2 线程结束");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t2.setName(" t2");
        t2.start();
    }      
}

在这里插入图片描述
在这里插入图片描述

5.使用synchronized 解决同步问题

所有需要修改hp的地方,有要建立在占有someObject的基础上
而对象 someObject在同一时间,只能被一个线程占有。 间接地,导致同一时间,hp只能被一个线程修改。

package multiplethread;  
import java.awt.GradientPaint;
import multiplethread.Hero;  
public class TestThread { 
    public static void main(String[] args) {
 
        final Object someObject = new Object();
         
        final Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 10000;
          
        int n = 10000;
  
        Thread[] addThreads = new Thread[n];
        Thread[] reduceThreads = new Thread[n];
          
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                     
                    //任何线程要修改hp的值,必须先占用someObject
                    synchronized (someObject) {
                        gareen.recover();
                    }
                     
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            addThreads[i] = t;
              
        }
          
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                    //任何线程要修改hp的值,必须先占用someObject
                    synchronized (someObject) {
                        gareen.hurt();
                    }
                     
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            reduceThreads[i] = t;
        }
          
        for (Thread t : addThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for (Thread t : reduceThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
          
        System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量是 %.0f%n", n,n,gareen.hp);        
    }     
}

在这里插入图片描述

6.使用hero对象作为同步对象

既然任意对象都可以用来作为同步对象,而所有的线程访问的都是同一个hero对象,索性就使用gareen来作为同步对象
进一步的,对于Hero的hurt方法,加上:
synchronized (this) {
}
表示当前对象为同步对象,即也是gareen为同步对象

package multiplethread;
public class Hero{
    public String name;
    public float hp;
    public int damage;
     
    //回血
    public void recover(){
        hp=hp+1;
    }
     
    //掉血
    public void hurt(){
        //使用this作为同步对象
        synchronized (this) {
            hp=hp-1;   
        }
    }
     
    public void attackHero(Hero h) {
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;
import java.awt.GradientPaint;
import multiplethread.Hero; 
public class TestThread {
    public static void main(String[] args) {
        final Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 10000;
          
        int n = 10000;
  
        Thread[] addThreads = new Thread[n];
        Thread[] reduceThreads = new Thread[n];
          
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                     
                    //使用gareen作为synchronized
                    synchronized (gareen) {
                        gareen.recover();
                    }
                     
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            addThreads[i] = t;
              
        }
          
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                    //使用gareen作为synchronized
                    //在方法hurt中有synchronized(this)
                    gareen.hurt();
                     
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            reduceThreads[i] = t;
        }
          
        for (Thread t : addThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for (Thread t : reduceThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
          
        System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量是 %.0f%n", n,n,gareen.hp);
          
    }    
}

在这里插入图片描述

7.在方法前,加上修饰符synchronized

在recover前,直接加上synchronized ,其所对应的同步对象,就是this
和hurt方法达到的效果是一样
外部线程访问gareen的方法,就不需要额外使用synchronized了

package multiplethread;
public class Hero{
    public String name;
    public float hp;
    public int damage;
     
    //回血
    //直接在方法前加上修饰符synchronized
    //其所对应的同步对象,就是this
    //和hurt方法达到的效果一样
    public synchronized void recover(){
        hp=hp+1;
    }
     
    //掉血
    public void hurt(){
        //使用this作为同步对象
        synchronized (this) {
            hp=hp-1;   
        }
    }
     
    public void attackHero(Hero h) {
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;
import java.awt.GradientPaint;
import multiplethread.Hero;
public class TestThread {
    public static void main(String[] args) {
        final Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 10000;
          
        int n = 10000;
  
        Thread[] addThreads = new Thread[n];
        Thread[] reduceThreads = new Thread[n];
          
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                     
                    //recover自带synchronized
                    gareen.recover();
                     
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            addThreads[i] = t;
              
        }
          
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(){
                public void run(){
                    //hurt自带synchronized
                    gareen.hurt();
                     
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            reduceThreads[i] = t;
        }
          
        for (Thread t : addThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for (Thread t : reduceThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
          
        System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量是 %.0f%n", n,n,gareen.hp);
          
    }     
}

在这里插入图片描述

8.线程安全的类

如果一个类,其方法都是有synchronized修饰的,那么该类就叫做线程安全的类

同一时间,只有一个线程能够进入 这种类的一个实例 的去修改数据,进而保证了这个实例中的数据的安全(不会同时被多线程修改而变成脏数据)

比如StringBuffer和StringBuilder的区别
StringBuffer的方法都是有synchronized修饰的,StringBuffer就叫做线程安全的类
而StringBuilder就不是线程安全的类

在这里插入图片描述

9.练习

在类方法前面加修饰符synchronized

在对象方法前,加上修饰符synchronized ,同步对象是当前实例。
那么如果在类方法前,加上修饰符 synchronized,同步对象是什么呢?

注:需要反射reflection的知识。

package com.how2java.test;

import org.junit.Test;

public class TestReflection {
	@Test //测试逻辑,打印类方法method1()的内容
    public void test1() throws Exception{
        Thread t1= new Thread(){
            public void run(){
                //调用method1
            	TestReflection.method1();
            }
        };
        t1.setName("第一个线程");
        t1.start();
  
  
        t1.join();
  
        Thread t2= new Thread(){
            public void run(){
                //调用method2
            	TestReflection.method2();
            }
        };
        t2.setName("第二个线程");
        t2.start();
  
    }
  
    public static void method1() {
  
        synchronized (TestReflection.class) {
            // 对于method1而言,同步对象是TestReflection.class,只有占用TestReflection.class才可以执行到这里
            System.out.println(Thread.currentThread().getName() + " 进入了method1方法");
            try {
                System.out.println("1运行5秒");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
  
                e.printStackTrace();
            }
            System.out.println("1111");
        }
    }
  
    public static synchronized void method2() {
        // 对于mehotd2而言,必然有个同步对象,通过观察发现,当某个线程在method1中,占用了TestReflection.class之后
        // 就无法进入method2,推断出,method2的同步对象,就是TestReflection.class
        System.out.println(Thread.currentThread().getName() + " 进入了method2方法");
        try {
            System.out.println("2运行5秒");
            Thread.sleep(100);
        } catch (InterruptedException e) {
  
            e.printStackTrace();
        }
  
    }
}

在这里插入图片描述
线程安全的MyStack

使用LinkedList实现Stack栈 中的MyStack类,改造为线程安全的类。

package charactor;
public class Hero  {
    public String name;
    public float hp;
    public int damage;
  
    public Hero() { 
    }
    public Hero(String name) {
        this.name = name;
    }
  
    public String toString() {
        return "Hero [name=" + name + "]\r\n";
    }
}
package multiplethread;
import charactor.Hero;
public interface Stack {
    //把英雄推入到最后位置
    public void push(Hero h);
    //把最后一个英雄取出来
    public Hero pull();
    //查看最后一个英雄
    public Hero peek();
}
package multiplethread;
import java.util.LinkedList;
import java.util.List;
import charactor.Hero;
/*
 *  使用LinkedList实现线程安全的Stack栈
 * */
public class MyStack implements Stack{
     
    //创建一个LinkedList对象就可以了
    LinkedList<Hero> ll=new LinkedList<Hero>();
 
    @Override
    //把英雄推入到最后位置
    public synchronized void push(Hero h) {
        //每次将传入的对象都添加到链表的w尾部
        ll.addLast(h);  
    }
 
    @Override
    //把最后一个英雄取出来
    public Hero pull() {
    	synchronized (this) {
	        //删除链表的尾部并返回
	        return ll.removeLast();
    	}
    }
 
    @Override
    //查看最后一个英雄
    public synchronized Hero peek() {
        //查看链表的尾部并返回
        return ll.getLast();
    }
     
    @Override
    public String toString() {
    	synchronized (this) {
    		return ll.toString();
    	}
    }
 
    public static void main(String[] args) {
        //创建MyStack对象
        MyStack ms=new MyStack();
         
        //往MyStack对象中添加英雄
        ms.push(new Hero("hero1"));
        ms.push(new Hero("hero2"));
        ms.push(new Hero("hero3"));
        ms.push(new Hero("hero4"));
        ms.push(new Hero("hero5"));
        System.out.println("MyStack中的英雄顺序为:\n"+ms);
         
        System.out.println("MyStack中最后一个英雄是:"+ms.peek());
         
        //取出最后一个英雄
        Hero h=ms.pull();
        System.out.println("把MyStack取出之后的英雄顺序为:\n"+ms);   
    }
}

在这里插入图片描述

五.线程安全的类

常见的线程安全相关的面试题。

1.HashMap和Hashtable的区别

HashMap和Hashtable都实现了Map接口,都是键值对保存数据的方式
区别1:
HashMap可以存放 null
Hashtable不能存放null
区别2:
HashMap不是线程安全的类
Hashtable是线程安全的类
在这里插入图片描述
在这里插入图片描述

2.StringBuffer和StringBuilder的区别

StringBuffer 是线程安全的
StringBuilder 是非线程安全的

所以当进行大量字符串拼接操作的时候,如果是单线程就用StringBuilder会更快些,如果是多线程,就需要用StringBuffer 保证数据的安全性。
非线程安全的为什么会比线程安全的 快? 因为不需要同步嘛,省略了些时间
在这里插入图片描述
在这里插入图片描述

3.ArrayList和Vector的区别

区别在于,ArrayList是非线程安全的,而Vector是线程安全的类。

ArrayList类的声明:
在这里插入图片描述
在这里插入图片描述
Vector类的声明:
在这里插入图片描述
在这里插入图片描述

4.把非线程安全的集合转换为线程安全

ArrayList是非线程安全的,换句话说,多个线程可以同时进入一个ArrayList对象的add方法。
借助Collections.synchronizedList,可以把ArrayList转换为线程安全的List。

与此类似的,还有HashSet,LinkedList,HashMap等等非线程安全的类,都可以通过工具类Collections转换为线程安全的。

package multiplethread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestThread {
	public static void main(String[] args) {
		List<Integer> list1=new ArrayList<>();
		List<Integer> list2=Collections.synchronizedList(list1);
	}
}

5.练习-线程安全的MyStack

借助把非线程安全的集合转换为线程安全,用另一个方式完成 练习-线程安全的MyStack。

package charactor;
public class Hero  {
    public String name;
    public float hp;
  
    public int damage;
  
    public Hero() {
  
    }
    public Hero(String name) {
 
        this.name = name;
    }    
    public Hero(String name, int hp, int damage) {
        this.name = name;
        this.hp = hp;
        this.damage = damage;
    }
    public String toString() {
        return "Hero [name=" + name +  "]\r\n";
    }
}

package multiplethread;
import charactor.Hero;
public interface Stack {
    //把英雄推入到最后位置
    public void push(Hero h);
    //把最后一个英雄取出来
    public Hero pull();
    //查看最后一个英雄
    public Hero peek();
}

package multiplethread;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
 
import charactor.Hero;
 
/*
 *  使用LinkedList实现Stack栈
 * 
 * 
 * 
 * */
public class MyStack implements Stack{
     
    //创建一个LinkedList对象就可以了
	//LinkedList<Hero> ll=new LinkedList<Hero>();
	
	//把LinkedList通过 Collections.synchronizedList转换成了一个线程安全的List
    List<Hero> heros = (List<Hero>) Collections.synchronizedList(new LinkedList<Hero>());
  
    //不需要在push上加synchronized修饰符
    //虽然多个线程可以同时进入push方法,但是调用heros.add方法的时候
    //同一时间,只有一个线程可以进入
    public void push(Hero h) {	
        heros.add(h);
    }
    
    //不用synchronized会出现线程不安全
    //这个问题怎么解决呢? 需要用到线程交互的知识。
    public synchronized Hero  pull() {
        return heros.remove(heros.size()-1);
    }
     
    public Hero peek() {
        return heros.get(heros.size()-1);
    }
    @Override
    public String toString() {
        return heros.toString();
    }
 
    public static void main(String[] args) {
        int n = 1000;
        final MyStack heroStack = new MyStack();
        Thread[] threads1 = new Thread[n];
        Thread[] threads2 = new Thread[n];
        for (int i = 0; i < n; i++) {
            final Hero h = new Hero("hero name " + i);
            System.out.println("压入 hero:" + h);
            Thread thread = new Thread(){
                public void run() {
                    heroStack.push(h);
                };
            };
            thread.start();
            threads1[i] = thread;
        }
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(){
                @Override
                public void run() {
                    Hero h = heroStack.pull();
                    System.out.println("弹出 hero" + h);
                };
            };
            thread.start();
            threads2[i] = thread;
        }
        for (Thread thread : threads1) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for (Thread thread : threads2) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         
        System.out.println(heroStack);//理论为null,否则线程不安全
    }
}

在这里插入图片描述
在这里插入图片描述

六.死锁

当业务比较复杂,多线程应用里有可能会发生死锁。

1.演示死锁

  1. 线程1 首先占有对象1,接着试图占有对象2
  2. 线程2 首先占有对象2,接着试图占有对象1
  3. 线程1 等待线程2释放对象2
  4. 与此同时,线程2等待线程1释放对象1

就会。。。一直等待下去,直到天荒地老,海枯石烂,山无棱 ,天地合。。。
在这里插入图片描述

package multiplethread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestThread {
	public static void main(String[] args) {
		final Hero ahri=new Hero();
		ahri.name="九尾妖狐";
		final Hero annie=new Hero();
		annie.name="安妮";
		Thread t1=new Thread(){
			public void run(){
				//占有九尾妖狐
				synchronized(ahri){
					System.out.println("t1 已占有九尾妖狐");
					try {
						停顿1000毫秒,另一个线程有足够的时间占有安妮
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("t1 试图占有安妮");
                    System.out.println("t1 等待中 。。。。");
                    synchronized(annie){
                    	System.out.println("do oo something");
                    }
				}
			}
		};
		t1.start();
		Thread t2=new Thread(){
			public void run(){
				//占有安妮
				synchronized(annie){
					System.out.println("t2 已占有安妮");
					try {
						//停顿1000毫秒,另一个线程有足够的时间占有九尾妖狐
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("t2 试图占有九尾妖狐");
					System.out.println("t2 等待中 。。。。");
					synchronized(ahri){
						System.out.println("do xx something");
					}
				}	
			}
		};
		t2.start();
	}
}

在这里插入图片描述

2.练习-死锁

3个同步对象a, b, c
3个线程 t1,t2,t3

故意设计场景,使这3个线程彼此死锁

package multiplethread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestThread {
	public static void main(String[] args) {
		final Hero a=new Hero();
		a.name="a";
		final Hero b=new Hero();
		b.name="b";
		final Hero c=new Hero();
		c.name="c";
		Thread t1=new Thread(){
			public void run(){
				//占有a
				synchronized(a){
					System.out.println("t1 已占有a");
					try {
						//停顿1000毫秒,另外的线程有足够的时间占有b,c
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("t1 试图占有b");
                    System.out.println("t1 等待中 。。。。");
                    synchronized(b){
                    	System.out.println("do something");
                    }
				}
			}
		};
		t1.start();
		Thread t2=new Thread(){
			public void run(){
				//占有b
				synchronized(b){
					System.out.println("t2 已占有b");
					try {
						//停顿1000毫秒,另外的线程有足够的时间占有a,c
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("t2 试图占有c");
					System.out.println("t2 等待中 。。。。");
					synchronized(c){
						System.out.println("do something");
					}
				}	
			}
		};
		t2.start();
		Thread t3=new Thread(){
			public void run(){
				//占有c
				synchronized(c){
					System.out.println("t3 已占有c");
					try {
						//停顿1000毫秒,另外的线程有足够的时间占有a,b
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("t3 试图占有a");
					System.out.println("t3 等待中 。。。。");
					synchronized(a){
						System.out.println("do something");
					}
				}	
			}
		};
		t3.start();
	}
}

在这里插入图片描述

七.交互

线程之间有交互通知的需求,考虑如下情况:
有两个线程,处理同一个英雄。
一个加血,一个减血。

减血的线程,发现血量=1,就停止减血,直到加血的线程为英雄加了血,才可以继续减血。

1.不好的解决方式

故意设计减血线程频率更高,盖伦的血量迟早会到达1
减血线程中使用while循环判断是否是1,如果是1就不停的循环,直到加血线程回复了血量
这是不好的解决方式,因为会大量占用CPU,拖慢性能

package multiplethread;
public class Hero{
    public String name;
    public float hp;
    public int damage;
     
    //回血
    //直接在方法前加上修饰符synchronized
    //其所对应的同步对象,就是this
    //和hurt方法达到的效果一样
    public synchronized void recover(){
        hp=hp+1;
    }
     
    //掉血
    public void hurt(){
        //使用this作为同步对象
        synchronized (this) {
            hp=hp-1;   
        }
    }
     
    public void attackHero(Hero h) {
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;
import multiplethread.Hero;
public class TestThread {
	public static void main(String[] args) {
		final Hero gareen=new Hero();
		gareen.name="盖伦";
		gareen.hp=616;
		Thread t1=new Thread(){
			public void run(){
				while(true){
					//因为减血更快,所以盖伦的血量迟早会到达1
                    //使用while循环判断是否是1,如果是1就不停的循环
                    //直到加血线程回复了血量
					while(gareen.hp==1){
						continue;
					}
					gareen.hurt();
					System.out.printf("t1 为%s 减血1点,减少血后,%s的血量是%.0f%n",gareen.name,gareen.name,gareen.hp);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		t1.start();
		Thread t2=new Thread(){
			public void run(){
				while(true){
					gareen.recover();
					System.out.printf("t2 为%s 回血1点,增加血后,%s的血量是%.0f%n",gareen.name,gareen.name,gareen.hp);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		t2.start();
	}
}

在这里插入图片描述
在这里插入图片描述
注:输出表现为:减血为1时,减血线程一直在循环中。之后加血(一直加血),减血线程还在循环???

因为血量到达1后while(gareen.hp==1){continue; }一直为true,也就是减血线程一直在while里,不会执行后面的减血操作。 这个涉及到程序的可见性问题 , 因为cpu跟内存速度差异大,所以cpu中有寄存器和缓存, 增加线程和减少线程在缓存中都有对应的hp,这在java内存模型中也被抽象叫为工作内存, 工作内存是线程独有的,某一时刻才会同步到主内存,所以才会出现减少线程中hp一直为1的情况,尽管增加线程已经给hp加血了,如果给hero类中hp加上volatile关键字就不会出现只增加的情况,volatile关键保证了不同线程对这个变量进行操作时的可见性,即一个线程修改了某个变量的值,这新值对其他线程来说是立即可见的,对于这个例子来说加了volatile后,增加和减少血量都会同步到主内存,减血线程会去主内存读取,所以不会上面的这个情况。

2.使用wait和notify进行线程交互

在Hero类中:hurt()减血方法:当hp=1的时候,执行this.wait()
this.wait()表示 让占有this的线程等待,并临时释放占有
进入hurt方法的线程必然是减血线程,this.wait()会让减血线程临时释放对this的占有。 这样加血线程,就有机会进入recover()加血方法了。

recover() 加血方法:增加了血量,执行this.notify();
this.notify() 表示通知那些等待在this的线程,可以苏醒过来了。 等待在this的线程,恰恰就是减血线程。 一旦recover()结束, 加血线程释放了this,减血线程,就可以重新占有this,并执行后面的减血工作。
在这里插入图片描述

package multiplethread;
public class Hero{
    public String name;
    public float hp;
     
    public int damage;
     
    //回血
    //直接在方法前加上修饰符synchronized
    //其所对应的同步对象,就是this
    //和hurt方法达到的效果一样
    public synchronized void recover(){
        hp=hp+1;
        System.out.printf("%s 回血1点,增加血后,%s的血量是%.0f%n", name, name, hp);
        // 通知那些等待在this对象上的线程,可以醒过来了,如第20行,等待着的减血线程,苏醒过来
        this.notify();
    }
    
    
     
    //掉血
    public synchronized void hurt(){
    	if (hp == 1) {
            try {
                // 让占有this的减血线程,暂时释放对this的占有,并等待
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    	hp = hp - 1;
        System.out.printf("%s 减血1点,减少血后,%s的血量是%.0f%n", name, name, hp);
    }
     
    public void attackHero(Hero h) {
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;
import multiplethread.Hero;
public class TestThread {
	public static void main(String[] args) {
		final Hero gareen=new Hero();
		gareen.name="盖伦";
		gareen.hp=616;
		Thread t1=new Thread(){
			public void run(){
				while(true){
					//无需循环判断
					/*while(gareen.hp==1){
						continue;
					}*/
					gareen.hurt();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		t1.start();
		Thread t2=new Thread(){
			public void run(){
				while(true){
					gareen.recover();
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		t2.start();
	}
}

在这里插入图片描述

3.关于wait、notify和notifyAll

留意wait()和notify() 这两个方法是什么对象上的?

public synchronized void hurt() {
  。。。
  this.wait();
  。。。
}
public synchronized void recover() {
   。。。
   this.notify();
}

这里需要强调的是,wait方法和notify方法,并**不是Thread线程上的方法**,它们是 Object上的方法。
因为所有的Object都可以被用来作为同步对象,所以准确的讲,wait和notify是同步对象上的方法。
wait()的意思是: 让占用了这个同步对象的线程,临时释放当前的占用,并且等待。 所以调用wait是有前提条件的,一定是在synchronized块里,否则就会出错。
notify() 的意思是,通知一个等待在这个同步对象上的线程,你可以苏醒过来了,有机会重新占用当前对象了。
notifyAll() 的意思是,通知所有的等待在这个同步对象上的线程,你们可以苏醒过来了,有机会重新占用当前对象了。

4.练习

线程交互

假设加血线程运行得更加频繁,英雄的最大血量是1000

设计加血线程和减血线程的交互,让回血回满之后,加血线程等待,直到有减血线程减血

package multiplethread;
public class Hero{
    public String name;
    public float hp;
     
    public int damage;
     
    //回血
    //直接在方法前加上修饰符synchronized
    //其所对应的同步对象,就是this
    //和hurt方法达到的效果一样
    public synchronized void recover(){
    	if(hp==1000){
    		try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
        hp=hp+1;
        System.out.printf("%s 回血1点,增加血后,%s的血量是%.0f%n", name, name, hp);
    }
    
    //掉血
    public synchronized void hurt(){        
            hp=hp-1;        
            System.out.printf("%s 减血1点,减少血后,%s的血量是%.0f%n", name, name, hp);
            this.notify();
    }
     
    public void attackHero(Hero h) {
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n", name, h.name, h.name, h.hp);
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;
import multiplethread.Hero;
public class TestThread {
	public static void main(String[] args) {
		final Hero gareen=new Hero();
		gareen.name="盖伦";
		gareen.hp=616;
		Thread t1=new Thread(){
			public void run(){
				while(true){
						gareen.recover();
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		t1.start();
		Thread t2=new Thread(){
			public void run(){
				while(true){
						gareen.hurt();
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		};
		t2.start();
	}
}

在这里插入图片描述
多线程交互

在上面的练习的基础上,增加回血线程到2条,减血线程到5条,同时运行。
运行一段时间,观察会发生的错误,分析错误原因,并考虑解决办法。

package multiplethread;
public class Hero{
    public String name;
    public float hp;
     
    public int damage;
     
    //回血
    //直接在方法前加上修饰符synchronized
    //其所对应的同步对象,就是this
    //和hurt方法达到的效果一样
    public synchronized void recover(){
    	if(hp==1000){
    		try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
        hp=hp+1;
        System.out.printf("%s 回血1点,增加血后,%s的血量是%.0f%n", name, name, hp);
    }
    
    
     
    //掉血
    public synchronized void hurt(){        
            hp=hp-1;        
            System.out.printf("%s 减血1点,减少血后,%s的血量是%.0f%n", name, name, hp);
            this.notify();
    }
     
    public void attackHero(Hero h) {
        h.hp-=damage;
        System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n", name, h.name, h.name, h.hp);
        if(h.isDead())
            System.out.println(h.name +"死了!");
    }
  
    public boolean isDead() {
        return 0>=hp?true:false;
    }
}
package multiplethread;
import multiplethread.Hero;
public class TestThread {
	public static void main(String[] args) {
		final Hero gareen=new Hero();
		gareen.name="盖伦";
		gareen.hp=616;
		Thread[] addThreads = new Thread[2];
        Thread[] reduceThreads = new Thread[5];
        for (int i = 0; i < addThreads.length; i++) {
			Thread t=new Thread(){
				public void run(){
					while(true){
							gareen.recover();
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			};
			t.start();
			addThreads[i] = t;
        }
        for (int i = 0; i < reduceThreads.length; i++) {
			Thread t=new Thread(){
				public void run(){
					while(true){
							gareen.hurt();
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			};
			t.start();
			reduceThreads[i]=t;
        }
        for (Thread t : addThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        for (Thread t : reduceThreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
	}
}

在这里插入图片描述
生产者消费者问题

生产者消费者问题是一个非常典型性的线程交互的问题。

  1. 使用栈来存放数据
    1.1 把栈改造为支持线程安全
    1.2 把栈的边界操作进行处理,当栈里的数据是0的时候,访问pull的线程就会等待。 当栈里的数据是200的时候,访问push的线程就会等待
  2. 提供一个生产者(Producer)线程类,生产随机大写字符压入到堆栈
  3. 提供一个消费者(Consumer)线程类,从堆栈中弹出字符并打印到控制台
  4. 提供一个测试类,使两个生产者和三个消费者线程同时运行,结果类似如下 :

在这里插入图片描述

package multiplethread;
public interface Stack {
    //把字符推入到最后位置
    public void push(char c);
    //把最后一个字符取出来
    public char  pull();
    //查看最后一个字符
    public char peek();
}
package multiplethread;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
 
/*
 *  使用LinkedList实现Stack栈
 * 
 * 
 * 
 * */
public class MyStack implements Stack{
 
	LinkedList<Character> ms;
    public MyStack() {
    	ms=new LinkedList<>();
    }
    public synchronized void push(char c) {	
    	while(ms.size() >= 200) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    	//每次将传入的对象都添加到链表的尾部
        ms.addLast(c);
        this.notifyAll();
    }
   
    public synchronized char  pull() {
    	 while (ms.isEmpty()) {
             try {
                 this.wait();
             } catch (InterruptedException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         }
         this.notifyAll();
       //删除链表的尾部并返回 
        return ms.removeLast();
    }
     
    public synchronized char peek() {
    	//查看链表的尾部并返回
        return ms.getLast();
    }
    @Override
    public String toString() {
        return ms.toString();
    }
}

package multiplethread;
//测试类
public class ConsumerAndProducer {
    // Consumer线程类
    static class Consumer extends Thread {
        private MyStack ms;
 
        private String name;
 
        public Consumer(MyStack ms, String name) {
            this.ms = ms;
            this.name = name;
        }
 
        public void run() {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.printf("%s 弹出:%s%n", name, ms.pull());
            }
        }
    }
 
    // Producer线程类
    static class Producer extends Thread {
        private MyStack ms;
 
        private String name;
 
        public Producer(MyStack ms, String name) {
            this.ms = ms;
            this.name = name;
        }
 
        public void run() {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                char c = (char) (Math.random() * ('Z'-'A'+1) + 'A');
                ms.push(c);
                System.out.printf("%s 压入:%s%n", name, c);
            }
        }
    }
 
    public static void main(String[] args) {
        MyStack ms = new MyStack();
        for (int i = 1; i < 3; i++)
            new Producer(ms, "Producer" + i).start();
 
        for (int i = 1; i < 4; i++)
            new Consumer(ms, "Consumer" + i).start();
    }
}

在这里插入图片描述

八.线程池

每一个线程的启动和结束都是比较消耗时间和占用资源的。
如果在系统中用到了很多的线程,大量的启动和结束动作会导致系统的性能变卡,响应变慢。
为了解决这个问题,引入线程池这种设计思想。
线程池的模式很像生产者消费者模式,消费的对象是一个一个的能够运行的任务。

1.线程池设计思路

线程池的思路和生产者消费者模型是很接近的。
1.准备一个任务容器
2.一次性启动10个 消费者线程
3.刚开始任务容器是空的,所以线程都wait在上面
4.直到一个外部线程往这个任务容器中扔了一个“任务”,就会有一个消费者线程被唤醒notify
5.这个消费者线程取出“任务”,并且执行这个任务,执行完毕后,继续等待下一次任务的到来。
6.如果短时间内,有较多的任务加入,那么就会有多个线程被唤醒,去执行这些任务。

在整个过程中,都不需要创建新的线程,而是循环使用这些已经存在的线程

在这里插入图片描述

2.开发一个自定义线程池

这是一个自定义的线程池,虽然不够完善和健壮,但是已经足以说明线程池的工作原理
缓慢的给这个线程池添加任务,会看到有多条线程来执行这些任务。
线程7执行完毕任务后,又回到池子里,下一次任务来的时候,线程7又来执行新的任务。

package multiplethread;

import java.util.LinkedList;

public class ThreadPool {
	
	//线程池大小
	int threadPoolSize;
	
	//任务容器
	LinkedList<Runnable> tasks=new LinkedList<Runnable>();
	
	//试图消费任务的线程
	public ThreadPool(){
		threadPoolSize = 10;
		//启动10个任务消费者线程
		synchronized(tasks){
			for(int i=0;i<threadPoolSize;i++){
				new TaskConsumeThread("任务消费者线程 " + i).start();
			}
		}
	}
	
	public void add(Runnable r){
		synchronized(tasks){
			tasks.add(r);
			// 唤醒等待的任务消费者线程
			tasks.notifyAll();
		}
	}
	class TaskConsumeThread extends Thread{
		public TaskConsumeThread(String name) {
            super(name);
        }
		Runnable task;
		
		public void run(){
			System.out.println("启动:"+this.getName());
			while(true){
				synchronized(tasks){
					 while (tasks.isEmpty()) {
	                    try {
	                        tasks.wait();
	                    } catch (InterruptedException e) {
	                        // TODO Auto-generated catch block
	                        e.printStackTrace();
	                    }
	                 }
					 task = tasks.removeLast();
                     // 允许添加任务的线程可以继续添加任务
                     tasks.notifyAll();
				}
				System.out.println(this.getName() + " 获取到任务,并执行");
                task.run();
			}
		}
	}

}
package multiplethread;
 
public class TestThread {
       
    public static void main(String[] args) {
        ThreadPool pool = new ThreadPool();
  
        for (int i = 0; i < 5; i++) {
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    //System.out.println("执行任务");
                    //任务可能是打印一句话
                    //可能是访问文件
                    //可能是做排序
                }
            };
             
            pool.add(task);
             
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
  
    }
           
}

在这里插入图片描述

3.测试线程池

创造一个情景,每个任务执行的时间都是1秒
刚开始是间隔1秒钟向线程池中添加任务
然后间隔时间越来越短,执行任务的线程还没有来得及结束,新的任务又来了。
就会观察到线程池里的其他线程被唤醒来执行这些任务

package multiplethread;
  
public class TestThread {
    public static void main(String[] args) {
        ThreadPool pool= new ThreadPool();
        int sleep=1000;
        while(true){
            pool.add(new Runnable(){
                @Override
                public void run() {
                    //System.out.println("执行任务");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
            try {
                Thread.sleep(sleep);
                sleep = sleep>100?sleep-100:sleep;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              
        }
          
    }
}

在这里插入图片描述

4.使用java自带线程池

java提供自带的线程池,而不需要自己去开发一个自定义线程池了。
线程池类ThreadPoolExecutor在包java.util.concurrent下

ThreadPoolExecutor threadPool= new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

第一个参数10 表示这个线程池初始化了10个线程在里面工作
第二个参数15 表示如果10个线程不够用了,就会自动增加到最多15个线程
第三个参数60 结合第四个参数TimeUnit.SECONDS,表示经过60秒,多出来的线程还没有接到活儿,就会回收,最后保持池子里就10个
第四个参数TimeUnit.SECONDS 如上
第五个参数 new LinkedBlockingQueue() 用来放任务的集合
execute方法用于添加新的任务

package multiplethread;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThread {
	public static void main(String[] args) {
		ThreadPoolExecutor threadPool=new  ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
		threadPool.execute(new Runnable(){
			   
            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("任务1");
            }
               
        });
	}
}

5.借助线程池同步查找文件内容

在 练习-同步查找文件内容 ,如果文件特别多,就会创建很多的线程。 改写这个练习,使用线程池的方式来完成。

初始化一个大小是10的线程池
遍历所有文件,当遍历到文件是.java的时候,创建一个查找文件的任务,把这个任务扔进线程池去执行,继续遍历下一个文件。

package multiplethread;
import java.io.File;
import java.io.FileReader;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
public class poolfile {
    //static ThreadPool pool = new ThreadPool();
 
    public static void searchFile(File file, String search) {
    	是否是文件
        if (file.isFile()) {
            if (file.getName().toLowerCase().endsWith(".java")) {
                 
                 
            	//方法一:另外创建一个任务线程
                 
                // searchFiletask task = new searchFiletask(file,search);
                // //添加任务
                // pool.add(task);
                //
                //
 
                //方法二:使用自己写的ThreadPool  
               /*Runnable task = new Runnable(){
	                 public void run(){
	                	 String fileContent = readFileContent(file);
	                	 if(fileContent.contains(search)){
	                		 System.out.printf( "线程: %s找到子目标字符串%s,在文件:%s%n",Thread.currentThread().getName(),search,file);
	                	 }
	                 }
                };
                pool.add(task);*/
                
                //方法三:使用java自带的ThreadPool
 
                ThreadPoolExecutor threadPool= new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
                    
                threadPool.execute(new Runnable(){
            
                    @Override
                    public void run() {
                         String fileContent = readFileContent(file);
                         if(fileContent.contains(search)){
                         System.out.printf( "线程: %s 子目标字符串%s,在文件:%s%n",Thread.currentThread().getName(),search,file);
                    }
                    }      
                });
            
            }
        }
        if (file.isDirectory()) {
            File[] fs = file.listFiles();
            for (File f : fs) {
                searchFile(f, search);
            }
        }
    }
 
    public static String readFileContent(File file) {
        try (FileReader fr = new FileReader(file)) {
            char[] all = new char[(int) file.length()];
            fr.read(all);
            return new String(all);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return null;
        }
 
    }
 
    public static void main(String[] args) {
        File folder = new File("D:\\LOLFolder");
        searchFile(folder, "sfsos");
    }
}

在这里插入图片描述

九. Lock对象

与synchronized类似的,lock也能够达到同步的效果。

1.回忆 synchronized 同步的方式

当一个线程占用 synchronized 同步对象,其他线程就不能占用了,直到释放这个同步对象为止。
在这里插入图片描述

2.使用Lock对象实现同步效果

Lock是一个接口,为了使用一个Lock对象,需要用到

Lock lock = new ReentrantLock();

与 **synchronized (someObject) 类似的,lock() **方法,表示当前线程占用lock对象,一旦占用,其他线程就不能占用了。
与 synchronized 不同的是,一旦synchronized 块结束,就会自动释放对someObject的占用。 lock却必须调用unlock方法进行手动释放,为了保证释放的执行,往往会把unlock() 放在finally中进行。

package multiplethread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestThread {
	public static String now(){
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}
	public static void log(String msg){
		System.out.printf("%s %s %s %n",now(),Thread.currentThread().getName(),msg);
	}
	public static void main(String[] args) {
		Lock lock=new ReentrantLock();
		
		Thread t1=new Thread(){
			public void run(){
				try {
					log("线程启动");
					log("试图占用对象:lock");
					lock.lock();
					log("占用对象:lock");
					log("进行5秒的业务操作");
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					log("释放对象:lock");
					lock.unlock();
				}
				log("线程结束");
			}
		};
		t1.setName("t1");
		t1.start();
		 try {
	            //先让t1飞2秒
	            Thread.sleep(2000);
	    } catch (InterruptedException e1) {
	            // TODO Auto-generated catch block
	            e1.printStackTrace();
	    }
		 Thread t2 = new Thread() {
			 
	            public void run() {
	                try {
	                    log("线程启动");
	                    log("试图占有对象:lock");
	 
	                    lock.lock();
	 
	                    log("占有对象:lock");
	                    log("进行5秒的业务操作");
	                    Thread.sleep(5000);
	 
	                } catch (InterruptedException e) {
	                    e.printStackTrace();
	                } finally {
	                    log("释放对象:lock");
	                    lock.unlock();
	                }
	                log("线程结束");
	            }
	        };
	        t2.setName("t2");
	        t2.start();	 
		 
	}
}

在这里插入图片描述

3.trylock方法

synchronized 是不占用到手不罢休的,会一直试图占用下去。
与 synchronized 的钻牛角尖不一样,Lock接口还提供了一个trylock方法。
trylock会在指定时间范围内试图占用,占成功了,就啪啪啪。 如果时间到了,还占用不成功,扭头就走~

注意: 因为使用trylock有可能成功,有可能失败,所以后面unlock释放锁的时候,需要判断是否占用成功了,如果没占用成功也unlock,就会抛出异常

package multiplethread;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class TestThread {
 
    public static String now() {
        return new SimpleDateFormat("HH:mm:ss").format(new Date());
    }
 
    public static void log(String msg) {
        System.out.printf("%s %s %s %n", now() , Thread.currentThread().getName() , msg);
    }
    
    public static void main(String[] args) {
		Lock lock=new ReentrantLock();
		
		Thread t1=new Thread(){
			public void run(){
				boolean locked=false;   
                try {
                	log("线程启动");
                    log("试图占有对象:lock");
					locked=lock.tryLock(1,TimeUnit.SECONDS);
					if(locked){
                        log("占有对象:lock");
                        log("进行5秒的业务操作");
                        Thread.sleep(5000);
                    }
                    else{
                        log("经过1秒钟的努力,还没有占有对象,放弃占有");
                    }
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
                    
                   if(locked){
                       log("释放对象:lock");
                       lock.unlock();
                   }
               }
               log("线程结束");
			}
		};
		t1.setName("t1");
        t1.start();
        try {
            //先让t1飞2秒
            Thread.sleep(2000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Thread t2 = new Thread() {
        	 
            public void run() {
                boolean locked = false;
                try {
                    log("线程启动");
                    log("试图占有对象:lock");
 
                    locked = lock.tryLock(1,TimeUnit.SECONDS);
                    if(locked){
                        log("占有对象:lock");
                        log("进行5秒的业务操作");
                        Thread.sleep(5000);
                    }
                    else{
                        log("经过1秒钟的努力,还没有占有对象,放弃占有");
                    }
 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                     
                    if(locked){
                        log("释放对象:lock");
                        lock.unlock();
                    }
                }
                log("线程结束");
            }
        };
        t2.setName("t2");
        t2.start();
	}
}

在这里插入图片描述

4.线程交互

使用synchronized方式进行线程交互,用到的是同步对象的wait,notify和notifyAll方法
Lock也提供了类似的解决办法,首先通过lock对象得到一个Condition对象,然后分别调用这个Condition对象的:await, signal,signalAll 方法。

注意: 不是Condition对象的wait,nofity,notifyAll方法,是await,signal,signalAll。

package multiplethread;
  
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
  
public class TestThread {
  
    public static String now() {
        return new SimpleDateFormat("HH:mm:ss").format(new Date());
    }
  
    public static void log(String msg) {
        System.out.printf("%s %s %s %n", now() , Thread.currentThread().getName() , msg);
    }
    public static void main(String[] args) {
    	Lock lock=new ReentrantLock();
    	Condition condition=lock.newCondition();
    	
    	Thread t1=new Thread(){
    		public void run(){		
                try {
                	log("线程启动");
        			log("试图占用对象:lock");
        			lock.lock();
        			log("占用对象:lock");
        			log("进行5秒的业务操作");
					Thread.sleep(5000);
					log("临时释放对象 lock, 并等待");
                    condition.await();
                    log("重新占有对象 lock,并进行5秒的业务操作");
                    Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
                    log("释放对象:lock");
                    lock.unlock();
                }
                log("线程结束");		
    		}
    	};
    	t1.setName("t1");
        t1.start();
        try {
            //先让t1飞2秒
            Thread.sleep(2000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Thread t2 = new Thread() {	  
            public void run() {
                try {
                    log("线程启动");
                    log("试图占有对象:lock");
  
                    lock.lock();
  
                    log("占有对象:lock");
                    log("进行5秒的业务操作");
                    Thread.sleep(5000);
                    log("唤醒等待中的线程");
                    condition.signal();
  
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    log("释放对象:lock");
                    lock.unlock();
                }
                log("线程结束");
            }
        };
        t2.setName("t2");
        t2.start();
	}
}

在这里插入图片描述

5.总结Lock和synchronized的区别

1.Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现,Lock是代码层面的实现。
2.Lock可以选择性的获取锁,如果一段时间获取不到,可以放弃。synchronized不行,会一根筋一直获取下去。 借助Lock的这个特性,就能够规避死锁,synchronized必须通过谨慎和良好的设计,才能减少死锁的发生。
3.synchronized在发生异常和同步块结束的时候,会自动释放锁。而Lock必须手动释放, 所以如果忘记了释放锁,一样会造成死锁。

6.练习

借助Lock,把MyStack修改为线程安全的类。

在练习-线程安全的MyStack 练习中,使用synchronized把MyStack修改为了线程安全的类。
接下来,借助Lock把MyStack修改为线程安全的类

借助tryLock 解决死锁问题

当多个线程按照不同顺序占用多个同步对象的时候,就有可能产生死锁现象。
死锁之所以会发生,就是因为synchronized 如果占用不到同步对象,就会苦苦的一直等待下去,借助tryLock的有限等待时间,解决死锁问题

package multiplethread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestThread {
	static Lock a = new ReentrantLock();
    static Lock b = new ReentrantLock();
    static Lock c = new ReentrantLock();
 
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            public void run() {
                boolean aLocked = false;
                boolean bLocked = false;
                try {
                    System.out.println("t1试图占有a");
                    System.out.println("t1等待中");
                    aLocked = a.tryLock(1, TimeUnit.SECONDS);
                    if (aLocked) {
                        System.out.println("t1已占有a");
                        Thread.sleep(500);
                        try {
                            System.out.println("t1试图占有b");
                            System.out.println("t1等待中");
                            bLocked = b.tryLock(1, TimeUnit.SECONDS);
                            if (bLocked)
                                System.out.println("t1已占有b");
                            else
                                System.out.println("经过1秒钟的努力,t1还没有占有b,放弃占有");
                        } finally {
                            if (bLocked) {
                                b.unlock();
                                System.out.println("t1已解除对b的占有");
                            }
                        }
 
                    } else
                        System.out.println("经过1秒钟的努力,t1还没有占有a,放弃占有");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (aLocked) {
                        a.unlock();
                        System.out.println("t1已解除对a的占有");
                    }
                }
            }
        };
        t1.start();
 
        Thread t2 = new Thread() {
            public void run() {
                boolean bLocked = false;
                boolean cLocked = false;
                try {
                    System.out.println("t2试图占有b");
                    System.out.println("t2等待中");
                    bLocked = b.tryLock(1, TimeUnit.SECONDS);
                    if (bLocked) {
                        System.out.println("t2已占有b");
                        Thread.sleep(500);
                        try {
                            System.out.println("t2试图占有c");
                            System.out.println("t2等待中");
                            cLocked = c.tryLock(1, TimeUnit.SECONDS);
                            if (cLocked)
                                System.out.println("t2已占有c");
                            else
                                System.out.println("经过1秒钟的努力,t2还没有占有c,放弃占有");
                        } finally {
                            if (cLocked) {
                                c.unlock();
                                System.out.println("t2已解除对c的占有");
                            }
                        }
 
                    } else
                        System.out.println("经过1秒钟的努力,t2还没有占有b,放弃占有");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (bLocked) {
                        b.unlock();
                        System.out.println("t2已解除对b的占有");
                    }
                }
            }
        };
        t2.start();
 
        Thread t3 = new Thread() {
            public void run() {
                boolean aLocked = false;
                boolean cLocked = false;
                try {
                    System.out.println("t3试图占有c");
                    System.out.println("t3等待中");
                    cLocked = c.tryLock(1, TimeUnit.SECONDS);
                    if (cLocked) {
                        System.out.println("t3已占有c");
                        Thread.sleep(500);
                        try {
                            System.out.println("t3试图占有a");
                            System.out.println("t3等待中");
                            aLocked = a.tryLock(1, TimeUnit.SECONDS);
                            if (aLocked)
                                System.out.println("t3已占有a");
                            else
                                System.out.println("经过1秒钟的努力,t3还没有占有a,放弃占有");
                        } finally {
                            if (aLocked) {
                                a.unlock();
                                System.out.println("t3已解除对a的占有");
                            }
                        }
 
                    } else
                        System.out.println("经过1秒钟的努力,t3还没有占有c,放弃占有");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (cLocked) {
                        c.unlock();
                        System.out.println("t3已解除对c的占有");
                    }
                }
            }
        };
        t3.start();
    }
}

在这里插入图片描述
生产者消费者问题

在练习-生产者消费者问题这个练习中,是用wait(), notify(), notifyAll实现了。
接下来使用Condition对象的:await, signal,signalAll 方法实现同样的效果

十. 原子访问

1.原子性操作概念

所谓的原子性操作即不可中断的操作,比如赋值操作

int i = 5;

原子性操作本身是线程安全的

但是 i++ 这个行为,事实上是有3个原子性操作组成的。
步骤 1. 取 i 的值
步骤 2. i + 1
步骤 3. 把新的值赋予i
这三个步骤,每一步都是一个原子操作,但是合在一起,就不是原子操作。就不是线程安全的。
换句话说,一个线程在步骤1 取i 的值结束后,还没有来得及进行步骤2,另一个线程也可以取 i的值了。
这也是分析同步问题产生的原因 中的原理。
i++ ,i–, i = i+1 这些都是非原子性操作。
只有int i = 1,这个赋值操作是原子性的。

2.AtomicInteger

JDK6 以后,新增加了一个包java.util.concurrent.atomic,里面有各种原子类,比如AtomicInteger
而AtomicInteger提供了各种自增,自减等方法,这些方法都是原子性的。 换句话说,自增方法 incrementAndGet 是线程安全的,同一个时间,只有一个线程可以调用这个方法。

package multiplethread;  
import java.util.concurrent.atomic.AtomicInteger;  
public class TestThread {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicI =new AtomicInteger();
        int i = atomicI.decrementAndGet();
        int j = atomicI.incrementAndGet();
        int k = atomicI.addAndGet(3);   
    }  
}

在这里插入图片描述

3.同步测试

分别使用基本变量的非原子性的**++运算符和 原子性的AtomicInteger对象的 incrementAndGet** 来进行多线程测试。

package multiplethread;

import java.util.concurrent.atomic.AtomicInteger;

public class TestThread {
	private static int value=0;
	private static AtomicInteger atomicValue=new AtomicInteger();
	public static void main(String[] args) {
		int number = 100000;
        Thread[] ts1 = new Thread[number];
        for (int i = 0; i < number; i++) {
            Thread t =new Thread(){
                public void run(){
                    value++;
                }
            };
            t.start();
            ts1[i] = t;
        }
         
        //等待这些线程全部结束
        for (Thread t : ts1) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }    
        System.out.printf("%d个线程进行value++后,value的值变成:%d%n", number,value);
        
        Thread[] ts2 = new Thread[number];
        for (int i = 0; i < number; i++) {
            Thread t =new Thread(){
                public void run(){
                    atomicValue.incrementAndGet();
                }
            };
            t.start();
            ts2[i] = t;
        }
         
        //等待这些线程全部结束
        for (Thread t : ts2) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.printf("%d个线程进行atomicValue.incrementAndGet();后,atomicValue的值变成:%d%n", number,atomicValue.intValue());
	}
}

在这里插入图片描述

4.练习-使用AtomicInteger来替换Hero类中的synchronized

给Hero的方法加上修饰符synchronized 这个知识点中,通过给hurt和 recover方法加上synchronized来达到线程安全的效果。
这一次换成使用AtomicInteger来解决这个问题
提示:int基本类型对应的是AtomicInteger,但是float基本类型没有对应的AtomicFloat。 所以在这个练习中,把hp改为AtomicInteger即可。

package multiplethread;

import java.util.concurrent.atomic.AtomicInteger;

public class Hero{
	   public String name;
	   public AtomicInteger hp= new AtomicInteger();
	   public int damage;
	   public void hurt(){
	       hp.decrementAndGet();
	   }
	   public void recover(){
	       hp.incrementAndGet();
	   }
	   public void attackHero(Hero h){
	       hp.addAndGet(-damage);
	       System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
	       if(h.isDead())
	           System.out.println(h.name +"死了!");
	   }
	   public boolean isDead() {
	       return 0>=hp.intValue()?true:false;
	   }
	   public static void main(String[] args) {
		   final Hero gareen = new Hero();
	        gareen.name = "盖伦";
	        gareen.hp.set(10000);
	 
	        int n = 10000;
	 
	        Thread[] addThreads = new Thread[n];
	        Thread[] reduceThreads = new Thread[n];
	 
	        for (int i = 0; i < n; i++) {
	            Thread t = new Thread() {
	                public void run() {
	                    gareen.recover();
	 
	                    try {
	                        Thread.sleep(100);
	                    } catch (InterruptedException e) {
	                        // TODO Auto-generated catch block
	                        e.printStackTrace();
	                    }
	                }
	            };
	            t.start();
	            addThreads[i] = t;
	 
	        }
	 
	        for (int i = 0; i < n; i++) {
	            Thread t = new Thread() {
	                public void run() {
	                    gareen.hurt();
	 
	                    try {
	                        Thread.sleep(100);
	                    } catch (InterruptedException e) {
	                        // TODO Auto-generated catch block
	                        e.printStackTrace();
	                    }
	                }
	            };
	            t.start();
	            reduceThreads[i] = t;
	        }
	 
	        for (Thread t : addThreads) {
	            try {
	                t.join();
	            } catch (InterruptedException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }
	        }
	        for (Thread t : reduceThreads) {
	            try {
	                t.join();
	            } catch (InterruptedException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }
	        }
	 
	        System.out.printf("%d个增加线程和%d个减少线程结束后%n盖伦的血量是 %.0f%n", n, n, gareen.hp.floatValue()); 
	}
}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值