首页 > 编程语言 > 详细

常见线程相关操作(一)

时间:2019-07-16 00:52:56      阅读:101      评论:0      收藏:0      [点我收藏+]
一.ThreadLocal
  ThreadLocal主要存储变量值  而这个值仅此线程操作,对其它的线程操作不影响,直接上代码:
public class UseThreadLocal {

    static ThreadLocal<Integer> threadLaocl = new ThreadLocal<Integer>(){

        @Override
        protected Integer initialValue() {
            return 5;
        }
    };

    /**
     * 运行3个线程
     */
    public void StartThreadArray(){
        Thread[] runs = new Thread[3];
        for(int i=0;i<runs.length;i++){
            runs[i]=new Thread(new TestThread(i));
        }
        for(int i=0;i<runs.length;i++){
            runs[i].start();
        }
    }
    
    /**
     *类说明:测试线程,线程的工作是将ThreadLocal变量的值变化,并写回,看看线程之间是否会互相影响
     */
    public static class TestThread implements Runnable{
        int id;
        public TestThread(int id){
            this.id = id;
        }
        public void run() {
            System.out.println(Thread.currentThread().getName()+":start");
            Integer s = threadLaocl.get();//获得变量的值
            s = s+id;
            threadLaocl.set(s);
            System.out.println(Thread.currentThread().getName()+":"
            +threadLaocl.get());
            //threadLaocl.remove();
        }
    }

    public static void main(String[] args){
        UseThreadLocal test = new UseThreadLocal();
        test.StartThreadArray();
    }
}

返回的结果为:技术分享图片

结论:不同的线程操作修改同一static修饰的ThreadLocal对象,然后对其它线程的值并没有影响

 二.等待和通知的标准范式

 等待方:

  1.获取锁

  2.循环里判断是否满足,不满足调用wait方法

  3.条件满足执行业务逻辑

通知方:

  1:获取锁

  2:改变条件

  3:唤醒所有等待对象的线程

三:notify与notifyAll

notifyAll是唤醒所有在等待的线程,notify是唤醒单个线程

上代码:

//等待方,等待条件满足做业务操作
public synchronized void waitSite() {
        while (CITY.equals(this.site)) {
            try {
                System.out.println("Thread:" + Thread.currentThread().getId() + " is wait! this city =" + this.site);
                wait();
                System.out.println("Thread:" + Thread.currentThread().getId() + " is be noify! this city =" + this.site);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("满足条件!wait结束!执行业务操作......");
    }
// 通知方:变化地点,然后通知处于wait状态并需要处理地点的线程进行业务处理
    public synchronized void changeSite() {
        this.site = "shenzhen";
        System.out.println("site条件改变!通知线程 name:"+Thread.currentThread().getName()+",做唤醒线程操作!");
        notifyAll();
    }
//main函数做的操作
public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 3; i++) {
            //三个线程监控快递地点变化
            new waitSiteThread().start();
            //三个线程监控快递距离变化
            new waitKmThread().start();
        }
        Thread.sleep(1000);
        express.changeSite();
    }

run的结果为:

技术分享图片

以上省略部分代码(跟site操作的代码完全类似)

分析:以上模拟代码满足等待通知的标准范式,方法加锁,进行应用操作时,首先应获取锁(如果没有加锁会报错),notifyAll唤醒当前的所有等待线程.

 

 

 

 

 

 

 

 

 

 

 

 

常见线程相关操作(一)

原文:https://www.cnblogs.com/tpqblogs/p/11192354.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!