多线程之间共享变量问题
发布日期:2022-04-11 08:52:48 浏览次数:7 分类:技术文章

本文共 2366 字,大约阅读时间需要 7 分钟。

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.concurrent.ScheduledThreadPoolExecutor;

public class test1 {
    
    
    
        tool  t3o= new tool();
        
    
    static class Scheduledtest extends Thread {
        private int i;

        public Scheduledtest(int in) {

                this.i = in;
            }

        @Override

        public void run() {
                while (true) {
                        try {
                                this.sleep(2000);
                            } catch (InterruptedException E) {
                                E.printStackTrace();
                            }
                        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        
                        System.out.println("2000");
                        
                        new tool().test22();
                    
                    }
            }
    }

static class ScheduledThread extends Thread {

        private int i;
        String str = "x2000;y3000;z4000;f500;";

        public ScheduledThread(int in) {

                this.i = in;
            }

        @Override

        public void run() {
            
            while(true){
                
                        try {
                                this.sleep(2000);
                            } catch (InterruptedException E) {
                                E.printStackTrace();
                            }
                        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        
                    
                        
                        
                        new tool().test(str);
                        
                        }
                        
                    }
            
    }

                public static void main(String[] args) {

                    
                        //String  sstr = "x222;y777;z300;f333;";
                    
                    //tool  to4 = new tool();
                    
                //    to4.test(sstr);
                    
                    //to4.test22();
                    
                    
                        ScheduledThreadPoolExecutor atpe = new ScheduledThreadPoolExecutor(5);//设置线程个数
                        
                                atpe.execute(new ScheduledThread(1));//普通的提交方式,只提交一次,执行结束,线程不会退出。
                                
                            atpe.execute(new Scheduledtest(1));
                            
                    }
            }
        这个是多线程的主函数

 

这个下面是调用方法

import java.util.*;

import java.util.concurrent.LinkedBlockingDeque;

public   class tool

{

            

        //StringBuffer sbf= new StringBuffer();
        //LinkedList  linlist = new LinkedList();
        
        LinkedBlockingDeque<Character> linkedBlockingDeque = new LinkedBlockingDeque<>();
        
        public void test(String dd){
        
            //StringBuffer sbf = new StringBuffer();
            
        for(int i=0;i<dd.length();i++){
            
            char ch=dd.charAt(i);
            
                linkedBlockingDeque.add(ch);
            
                
        }
        
            //System.out.println(linlist);
            
    
    }
    
    
    
    
        public  void test22(){
        
                String sbf = linkedBlockingDeque.toString();
            
        System.out.println(sbf);
        //System.out.println(linlist);
        
        int ztt=0;
        for(int i=0;i<sbf.length();i++){
            
            char ch22=sbf.charAt(i);
            
            switch(ch22){
                case 'x':   ztt=1; System.out.println("x");      break;
               case 'y':   ztt=2; System.out.println("y");      break;
               case 'z':   ztt=3; System.out.println("z");      break;
               case 'f':   ztt=4; System.out.println("f");      break;
               default:break;
            }
            
            if(ch22>='0'&& ch22<='9'){
                
                switch(ztt){
                    
                    case 1: System.out.println(ch22);break;
                        case 2: System.out.println(ch22);break;
                        case 3: System.out.println(ch22);break;
                        case 4: System.out.println(ch22);break;
                        default:break;
                }
                
            }
            
        }
        
        
        
    }
    
    
}

 

在运行中,另一个方法不能使用公共变量的值,也就是上一个方法更新的数据,在调用这个方法时,数据没有

转载地址:https://blog.csdn.net/weixin_44689973/article/details/123661016 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:多线程之间通信及线程池
下一篇:多线程之锁优化

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月29日 07时20分18秒