多线程之间共享变量问题
发布日期: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秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
信息标记与信息提取
2019-04-26
各大网站CSS初始化代码
2019-04-26
正则表达式的基本用法
2019-04-26
Python的Re库(正则表达式)基本用法
2019-04-26
Scrapy爬虫框架
2019-04-26
Anaconda
2019-04-26
NumPy库入门
2019-04-26
简单的留言板网页
2019-04-26
如何快速的搭建Apache+MySQL+PHP+PERL的环境
2019-04-26
初识JavaScript
2019-04-26
JavaScript的常用互动方法
2019-04-26
JavaScript的DOM操作
2019-04-26
JavaScript的事件响应与网页交互
2019-04-26
JavaScript实现的网页计算器功能
2019-04-26
英语四六级考试忘记准考证?怎么办?
2019-04-26
JavaScript内置对象
2019-04-26
JavaScript的游览器对象
2019-04-26
DOM对象,控制HTML对象
2019-04-26
制作一个表格,显示班级的学生信息
2019-04-26
JavaScript的选项卡操作
2019-04-26