zoukankan      html  css  js  c++  java
  • 多线程并发的问题解决方案

    多线程并发的问题解决方案

    package map;

    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.Map;

    public class TestConCurrentMap {
    static int index;
    public static void main(String[] args) {
    execute();
    }

    public static synchronized void execute(){
    final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < 10; i++) {
    new Thread(new Runnable() {
    public void run() {
    map.put(index++, index);
    while(true){
    System.out.println(Thread.currentThread().getName()+":"+map);
    try {
    Thread.sleep(1000);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }).start();
    }
    }
    }

    ----------------------------------------------------------------------------------------------------------
    // @Test
    public void testIsClose4WBDMultiThread(){
    System.out.println("多线程测试开始。。。。。。。。");
    Runnable task = new Runnable() {

    @Override
    public void run() {
    try {
    System.out.println("进入task任务");
    TradingHourServiceProvider provider = TradingHourServiceProvider.get();
    boolean b = provider.isClose4WBD(DateUtil.toDate("2017-09-06", DateUtil.PATTERN_DATE2));
    System.out.println(b);
    // Map<CacheType, String> cache = ConfigCache.get().getCache();
    } catch (GenericTsException e) {
    e.printStackTrace();
    }
    }
    };
    Map<Thread, String> map = new HashMap<Thread,String>();
    for(int i =0;i<20;i++){
    Thread t = new Thread(task);
    t.currentThread().setName("线程"+i);
    map.put(t, t.getName());
    }

    Set<Thread> threadSet = map.keySet();
    for (Thread t : threadSet) {
    t.start();
    }

    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    System.out.println("多线程测试结束。。。。。。。。");
    }

    @Test
    public void testHashMapMultiThread(){
    System.out.println("多线程测试开始。。。。。。。。");
    Runnable task = new Runnable() {

    @Override
    public void run() {
    try {
    System.out.println("进入task任务");
    TradingHourServiceProvider provider = TradingHourServiceProvider.get();
    boolean b = provider.isClose4WBD(DateUtil.toDate("2017-09-06", DateUtil.PATTERN_DATE2));
    System.out.println(b);
    // Map<CacheType, String> cache = ConfigCache.get().getCache();
    } catch (GenericTsException e) {
    e.printStackTrace();
    }
    }
    };
    Map<Thread, String> map = new HashMap<Thread,String>();
    for(int i =0;i<8;i++){
    Thread t = new Thread(task);
    t.currentThread().setName("线程"+i);
    map.put(t, t.getName());
    }

    Set<Thread> threadSet = map.keySet();
    for (Thread t : threadSet) {
    t.start();
    }

    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    System.out.println("多线程测试结束。。。。。。。。");
    }

  • 相关阅读:
    CVE-2021-25646 Apache Druid远程命令执行漏洞复现
    CVE-2021-3156漏洞复现
    助力抗疫 Splashtop 远程控制软件限时免费
    Splashtop Business Access 的常见问题解答
    热烈祝贺 Splashtop 荣获“最佳远程办公解决方案”奖
    Mark Lee:Splashtop 如何成为最新的 10 亿美元估值技术独角兽
    Splashtop获5000万美元新投资 成为远程桌面行业独角兽
    热烈祝贺 Splashtop 赢得最佳远程桌面用户满意度得分
    [翻译系列]正则表达式简介
    tensorflow2.0 keras 迁移学习 删除预训练模型的最后一层(layer)
  • 原文地址:https://www.cnblogs.com/lhl-shubiao/p/8494593.html
Copyright © 2011-2022 走看看