多线程之间共享变量问题

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;
                }
                
            }
            
        }
        
        
        
    }
    
    
}

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星海露水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值