thread and share variable

1.share variable

The simplest way for threads to share their results is to use shared variables. They should also use synchronization to ensure that values are propagated correctly from

one thread to another and to prevent threads from seeing inconsistent intermediate results while another thread is updating several related data items.

2.All threads live in the same memory space

As we discussed earlier, threads have a lot in common with processes, except that they share the same process context, including memory, with other threads in the same process. This is a tremendous convenience, but also a significant responsibility. Threads can easily exchange data among themselves simply by accessing shared variables (static or instance fields), but threads must also ensure that they access shared variables in a controlled manner, lest they step on each other's changes.

3.Synchronization for controlled access

The Java language provides two keywords for ensuring that data can be shared between threads in a controlled manner: synchronized and volatile.

Synchronized has two important meanings: it ensures that only one thread executes a protected section of code at one time (mutual exclusion or mutex ), and it ensures
that data changed by one thread is visible to other threads (visibility of changes).锁的排他性和内存的可见性。

Synchronized 关键字,使得某个时间点只有一个线程进入受Synchronized保护的程序块;并且使得线程工作内存中缓存的共享变量的副本的值失效,当线程进入Synchronized块时,必须从主内存中load最新的值;当线程退出Synchronized块时候,必须把缓存中的值刷新到主内存中。

Without synchronization, it is easy for data to be left in an inconsistent state. For example, if one thread is updating two related values (say, the position and velocity
of a particle), and another thread is reading those two values, it is possible that the second thread could be scheduled to run after the first thread has written one value
but not the other, thus seeing one old and one new value. Synchronization allows us to define blocks of code that must run atomically, in which they appear to execute in
an all-or-nothing manner, as far as other threads can tell.The atomic execution or mutual exclusion aspect of synchronization is similar to the concept of critical sections in other operating environments.

4.Volatile关键字

Volatile is simpler than synchronization and is suitable only for controlling access to single instances of primitive variables -- integers, booleans, and so on. When
a variable is declared volatile, any write to that variable will go directly to main memory, bypassing the cache, while any read of that variable will come directly from
main memory, bypassing the cache. This means that all threads see the same value for a volatile variable at all times.

注意:Volatile关键字只能保证内存的可见性,不能保证原子性操作。

package j.threads;


/**
 * 
 * @author Administrator
 * 该程序总共有21个线程,在主线程中启动了20个线程,这20个线程分别对共享变量累加1000次,最后得到的结果值
 * 我们期望是20 * 1000 = 20000;但是一旦运行这个程序我们不一定能得到20000.
 * 原因在于volatile只能保证内存的可见性,但是不能保证原子性操作,count++实际上有3个步骤read-modify-write
 * 不是一个独立的操作。
 *
 */
public class TestVolatile {

private static volatile int count = 0;

private static final int THREAD_COUNTS = 20;

//private static Object lock = new Object();

public static void increase(){
//synchronized(lock){
count++;
//}
}




public static void main(String[] args) throws InterruptedException{
Thread[] threads = new Thread[THREAD_COUNTS];

for(int i = 0; i < THREAD_COUNTS ;i++){
threads[i] = new Thread(new Runnable(){


@Override
public void run() {
for(int j = 0 ; j< 1000;j++)
increase();
}

});

threads[i].start();

}

for(int k = 0 ; k < THREAD_COUNTS; k++){
threads[k].join();
}

System.out.println(count);
}


}

5.对共享变量的复合操作必须要用lock来保证原子性操作。

Volatile is useful for ensuring that each thread sees the most recent value for a variable, but sometimes we need to protect access to larger sections of code, such
as sections that involve updating multiple variables. Synchronization uses the concepts of monitors, or locks, to coordinate access to particular blocks of code.
Every Java object has an associated lock. Java locks can be held by no more than one thread at a time. When a thread enters a synchronized block of code, the thread blocks and waits until the lock is available, acquires the lock when it becomes available, and then executes the block of code. It releases the lock when control exits the protected block of code, either by reaching the end of the block or when an exception is thrown that is not caught within the synchronized block.In this way, only one thread can execute a block protected by a given monitor at one time. The block can be considered atomic because, from the perspective of other threads, it appears to either have executed entirely or not at all.

6.Synchronized blocks

Because synchronization prevents multiple threads from executing a block at once,it has performance implications, even on uniprocessor systems. It is a good practice
to use synchronization around the smallest possible block of code that needs to be protected.

Access to local (stack-based) variables never need to be protected, because they are only accessible from the owning thread.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值