• 面试必会之多线程之间实现通讯


    1、什么是多线程之间通讯?

            多线程之间通讯,其实就是多个线程在操作同一个资源,但是操作的动作不同。

    2、线程之间通讯需求

            需求:第一个线程写入(input)用户,另一个线程取读取(out)用户,实现写一个,读一个的操作。

                            

    代码演示如下:

    共享资源实体类

    class Res {
        public String userSex;
        public String userName;
    }

    输入线程资源

    class InpThread extends Thread {
    	private Res res;
    
    	public InpThrad(Res res) {
                this.res = res;
    	}
    
    	@Override
    	public void run() {
                int count = 0;
                while (true) {
    		if (count == 0) {
    		    res.userName = "小明";
    		    res.userSex = "男";
    		} else {
    		    res.userName = "小红";
    		    res.userSex = "女";
    		}
    		count = (count + 1) % 2;
    	    }
    	}
    }

    输出线程

    class OutThread extends Thread {
    	private Res res;
    
    	public OutThread(Res res) {
    	    this.res = res;
    	}
    
    	@Override
    	public void run() {
                while (true) {
                    System.out.println(res.userName + "--" + res.userSex);
    	      }
    	}
    }

    运行代码

    public static void main(String[] args) {
            Res res = new Res();
    	InpThrad inpThrad = new InpThrad(res);
    	OutThread outThread = new OutThread(res);
    	intThrad.start();
    	outThread.start();
    }

    运行结果

          

    注意:此时数据会发生错乱,造成线程安全问题

    那么解决好线程安全问题,运行结果又会是怎样的呢?

    输入线程InpThread 类中加上synchronized

    class InpThread extends Thread {
    	private Res res;
    
    	public InpThrad(Res res) {
    		this.res = res;
    	}
    
    	@Override
    	public void run() {
                int count = 0;
    	    while (true) {
    		synchronized (res) {
    		if (count == 0) {
    		    res.userName = "小明";
    		    res.userSex = "男";
    		} else {
    		    res.userName = "小红";
    		    res.userSex = "女";
    		}
    		count = (count + 1) % 2;
    	        }
    	    }
            }
    }

    输出线程OutThread类中也加上synchronized

    class OutThread extends Thread {
    	private Res res;
    
    	public OutThread(Res res) {
    		this.res = res;
    	}
    
    	@Override
    	public void run() {
                while (true) {
    	        synchronized (res) {
    		    System.out.println(res.userName + "--" + res.userSex);
    		}
    	    }
    	}
    }

    那么此时的运行结果是怎样的呢?

         

    注意:InpThread 和 OutThread两个线程同时并行进行,在OutThread线程获取到CPU时会进行多次打印,并不会实现依次打印的效果

    那么怎样才能实现依次打印这种效果呢?

    3、wait()、notify()、notifyAll()方法

    wait()、notify()、notifyAll()是三个定义在Object类里的方法,可以用来控制线程的状态。

    • 如果对象调用了wait方法就会使持有该对象的线程把该对象的控制权交出去,然后处于等待状态。
    • 如果对象调用了notify方法就会通知某个正在等待这个对象的控制权的线程可以继续运行。
    • 如果对象调用了notifyAll方法就会通知所有等待这个对象控制权的线程继续运行。

    使用这三个方法就可以实现线程之间同步,达到了写一个读一个的效果。

    代码演示

    共享资源实体类

    class Res {
    	public String userSex;
    	public String userName;
    	//线程通讯标识
    	public boolean flag = false;
    }

    输入线程

    class InpThrad extends Thread {
    	private Res res;
    
    	public InpThrad(Res res) {
    		this.res = res;
    	}
    
    	@Override
    	public void run() {
                int count = 0;
                while (true) {
    	        synchronized (res) {
    		if (res.flag) {
    		    try {
    			// 当前线程变为等待,但是可以释放锁
    		        res.wait();
    		    } catch (Exception e) {
                            // TODO: handle exception
    		    }
    		  }
    		if (count == 0) {
    		    res.userName = "小明";
    		    res.userSex = "男";
    		} else {
    		    res.userName = "小红";
    		    res.userSex = "女";
    		}
    		count = (count + 1) % 2;
    		res.flag = true;
    		// 唤醒其他等待线程
    		res.notify();
    		}
    	    }
    	}
    }

    输出线程

    class OutThread extends Thread {
    	private Res res;
    
    	public OutThread(Res res) {
    	    this.res = res;
    	}
    
    	@Override
    	public void run() {
    	    while (true) {
    	        synchronized (res) {
    	        if (!res.flag) {
    		    try {
                            res.wait();
    		    } catch (Exception e) {
    		        // TODO: handle exception
                        }
    		}
    
    		System.out.println(res.userName + "--" + res.userSex);
    		res.flag = false;
    		res.notify();
    		}
    	    }
            }
    }

    运行结果

           

    此时线程之间同步,达到了写一个读一个的效果。

    4、 wait与sleep的区别?

    • 对于sleep()方法,我们首先要知道该方法是属于Thread类中的而wait()方法则是属于Object类中的。
    • sleep()方法导致了程序暂停执行指定的时间,让出cpu其他线程,但是的监控状态依然保持,当指定的时间到了又会自动恢复运行状态。
    • wait使用在多线程之间同步(和synchronized 一起使用)
    • 在调用sleep()方法的过程中,线程不会释放对象锁。而当调用wait()方法的时候,线程会释放对象锁,进入等待此对象的等待锁定池,只有此对象调用notify()方法后本线程才进入对象锁定池准备

    5、为什么wait和notify定义在object类中?

    因为我们在用多线程进行同步时,锁是由我们自己定义的,为了让所有类都能够使用,所以把wait和notify定义在object类中。

    6、Lock锁

    在 jdk1.5 之后,并发包中新增了 Lock 接口(以及相关实现类)用来实现锁功能,Lock接口提供了与 synchronized 关键字类似的同步功能,但需要在使用时手动获取锁和释放锁。

    Lock的写法

    Lock lock  = new ReentrantLock();
    lock.lock();
    try{
        //可能会出现线程安全的操作
    }finally{
        //一定要在finally中释放锁
        lock.unlock();
    }

    Lock 接口与 synchronized 关键字的区别

    • Lock 接口可以尝试非阻塞地获取锁  当前线程尝试获取锁,如果这一时刻锁没有被其他线程获取到,则成功获取并持有锁。
    • Lock 接口能被中断地获取锁 与 synchronized 不同,获取到锁的线程能够响应中断,当获取到的锁的线程被中断时,中断异常将会被抛出,同时锁会被释放。
    • Lock 接口在指定的截止时间之前获取锁,如果截止时间到了依旧无法获取锁,则返回。

     Condition用法

     Condition的功能类似于在传统线程技术中的wait()和notify()的功能

    Condition condition = lock.newCondition();
    res.condition.await();  //类似于wait
    res.condition.signal(); //类似于notify

    使用Lock同样可以实现上面的效果,代码演示如下:

    共享实体资源类

    class Res {
    	public String userSex;
    	public String userName;
    	// flag=false时out线程未读取值
    	public boolean flag = false;
    	// 定义一个锁
    	Lock lock = new ReentrantLock();
    	Condition condition = lock.newCondition();
    }

     输入线程

    class InpThread extends Thread {
    	private Res res;
    
    	public InpThread(Res res) {
    		this.res = res;
    	}
    	@Override
    	public void run() {
                int count = 0;
                while (true) {
    		try {
    		    res.lock.lock();
    		    if (res.flag) {
    			try {
    			    // 当前线程变为等待,但是可以释放锁
    			    // 类似于res.wait();
    			    res.condition.await();
    			} catch (Exception e) {
    
    			}
    		      }
    
    		    if (count == 0) {
    			    res.userName = "小明";
    			    res.userSex = "男";
    			} else {
    			    res.userName = "小红";
    			    res.userSex = "女";
    			}
    
    		    count = (count + 1) % 2;
    		    res.flag = true;
    		    // 唤醒当前线程 // 类似于res.notify();
    		    res.condition.signal();
    		} catch (Exception e) {
    		    // TODO: handle exception
    		} finally {
    		    res.lock.unlock(); //释放锁,一定要在finally中
    		}
    	    }
    	}
    }

    输出线程

    class OutThread extends Thread {
    	private Res res;
    
    	public OutThread(Res res) {
    		this.res = res;
    	}
    
    	@Override
    	public void run() {
                while (true) {
    		try {
    		res.lock.lock();
    		if (!res.flag) {
    		    try {
    		        res.condition.await();
    		    } catch (Exception e) {
    		        // TODO: handle exception
    		    }
    		}
    
    		System.out.println(res.userName+"--"+res.userSex);
    		res.flag = false;
    		res.condition.signal();
    	    } catch (Exception e) {
    
                } finally {
    		res.lock.unlock();  //释放锁,一定要在finally中
                }
    	}
        }
    }

    运行结果

           

    所以使用Lock同样也可以实现上面的效果。

  • 相关阅读:
    Django模型
    Django视图
    Django入门
    Python之安装第三方扩展库
    【转载】python计算文件的行数和读取某一行内容的实现方法
    easymock入门
    Head First Python之4持久存储
    Selenium2+python自动化之读取Excel数据(xlrd)
    Selenium2+python自动化之数据驱动(ddt)
    Head First Python之3文件与异常
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309329.html
Copyright © 2020-2023  润新知