• 多线程中范围内共享变量的概念与作用


    <span style="font-size:24px;">import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    /*
     * 线程范围内共享变量的概念与作用,创建三个线程,都访问了三个对象
     * 第一个对象设置值,第二第三个对象取值,同一个线程设置的值,只能被相同的线程获取
     */
    public class ThreadScopeShareDate {
    	private static int data = 0;
    	private static Map<Thread, Integer> threadData = new HashMap<Thread, Integer>();
    
    	public static void main(String[] args) {
    		for (int i = 0; i < 2; i++) {
    			new Thread(new Runnable() {
    
    				@Override
    				public void run() {
    					int data = new Random().nextInt();
    					System.out.println(Thread.currentThread().getName()
    							+ "has put data" + data);
    					threadData.put(Thread.currentThread(), data);
    					new A().get();
    					new B().get();
    				}
    			}).start();
    		}
    	}
    
    	static class A {
    		public void get() {
    			int data = threadData.get(Thread.currentThread());
    			System.out.println("A from" + Thread.currentThread().getName()
    					+ "has put data" + data);
    		}
    	}
    
    	static class B {
    		public void get() {
    			int data = threadData.get(Thread.currentThread());
    			System.out.println("B from" + Thread.currentThread().getName()
    					+ "has put data" + data);
    		}
    	}
    
    }
    </span>

  • 相关阅读:
    定时器的使用
    new LayoutParams 使用
    判断,日期是是昨天,前天 ,今天
    google推出的SwipeRefreshLayout下拉刷新用法
    Intent的Flag
    Eclipse Java注释模板设置详解
    Eclipse的模板设置代码
    Android如何在java代码中设置margin
    软键盘挡住输入框的解决方案
    Android自定义遮罩层设计
  • 原文地址:https://www.cnblogs.com/Rollins/p/4524890.html
Copyright © 2020-2023  润新知