synchronized

servlet不是线程安全的

半世苍凉 提交于 2019-12-08 09:46:30
在tomcat容器中,servlet默认是单例模式;如果实现ISingleThreadModule标记接口 则针对多个请求创建多个实例(最多20个),为了实现线程安全,可以: 1.使用局部变量 而不是实例变量 2.使用synchronized关键字 综上说明静态变量、实例变量、局部变量,三者使用范围或者说生命周期越大 则越不线程安全 来源: https://www.cnblogs.com/hzq3554055/p/12004630.html

Synchronized with GLThread in Android

坚强是说给别人听的谎言 提交于 2019-12-08 04:03:45
问题 I'm making a game in android using opengl-es, using multiple threads: class World{ protected static final AtomicInteger entityLock = new AtomicInteger(); private GameEntity entities[]; public World(){ // populate game world with entities // executed on main thread addEntity(new GameEntity("tank")); addEntity(new GameEntity("rifleman")); addEntity(new GameEntity("rifleman")); } void update(){ synchronized(entityLock){ for(int i = 0;i<entities.length;i++){ // move entity to new position //

Should I synchronize with self or with the method argument?

北慕城南 提交于 2019-12-08 03:57:14
问题 In a method like this, which do synchronize (i.e. self or thing )? - (BOOL)deleteThing:(MSThing *)thing error:(NSError **)error { @synchronized(self) { if (!thing) { return YES; } NSString *fileName = [[self thingDirectory] stringByAppendingPathComponent:thing.cacheInstanceName]; if (![[NSFileManager defaultManager] fileExistsAtPath:fileName]) { //... === OR === - (BOOL)deleteThing:(MSThing *)thing error:(NSError **)error { @synchronized(thing) { if (!thing) { return YES; } NSString *fileName

Program gets halted: wait() and notify()

ぃ、小莉子 提交于 2019-12-08 03:31:07
问题 I am trying to achieve this: Created two different threads, one prints odd numbers, one prints even numbers. Once one thread prints a number, it has to wait for the other thread and so on, that is one-after-other. To achieve this, i am using synchronized block along with wait() and notify(). I am creating a class whose's object will be used to pass to synchronized block in both the threads. Here is the code: --> This is used object which will be passed to synchronized block. package com.vipin

Java并发编程:synchronized

寵の児 提交于 2019-12-07 21:37:44
原文出处: 海子 虽然多线程编程极大地提高了效率,但是也会带来一定的隐患。比如说两个线程同时往一个数据库表中插入不重复的数据,就可能会导致数据库中插入了相同的数据。今天我们就来一起讨论下线程安全问题,以及Java中提供了什么机制来解决线程安全问题。 以下是本文的目录大纲: 一.什么时候会出现线程安全问题? 二.如何解决线程安全问题? 三.synchronized同步方法或者同步块 若有不正之处,请多多谅解并欢迎批评指正。 一.什么时候会出现线程安全问题? 在单线程中不会出现线程安全问题,而在多线程编程中,有可能会出现同时访问同一个资源的情况,这种资源可以是各种类型的的资源:一个变量、一个对象、一个文件、一个数据库表等,而当多个线程同时访问同一个资源的时候,就会存在一个问题: 由于每个线程执行的过程是不可控的,所以很可能导致最终的结果与实际上的愿望相违背或者直接导致程序出错。 举个简单的例子: 现在有两个线程分别从网络上读取数据,然后插入一张数据库表中,要求不能插入重复的数据。 那么必然在插入数据的过程中存在两个操作: 1)检查数据库中是否存在该条数据; 2)如果存在,则不插入;如果不存在,则插入到数据库中。 假如两个线程分别用thread-1和thread-2表示,某一时刻,thread-1和thread-2都读取到了数据X,那么可能会发生这种情况: thread

java Volatile/synchronization on arraylist

白昼怎懂夜的黑 提交于 2019-12-07 13:01:14
问题 My program looks like this: public class Main { private static ArrayList<T> list; public static void main(String[] args) { new DataListener().start(); new DataUpdater().start(); } static class DataListener extends Thread { @Override public void run() { while(true){ //Reading the ArrayList and displaying the updated data Thread.sleep(5000); } } } static class DataUpdater extends Thread{ @Override public void run() { //Continuously receive data and update ArrayList; } } } In order to use this

Proper use of volatile variables and synchronized blocks

[亡魂溺海] 提交于 2019-12-07 10:33:22
问题 I am trying to wrap my head around thread safety in java (or in general). I have this class (which I hope complies with the definition of a POJO) which also needs to be compatible with JPA providers: public class SomeClass { private Object timestampLock = new Object(); // are "volatile"s necessary? private volatile java.sql.Timestamp timestamp; private volatile String timestampTimeZoneName; private volatile BigDecimal someValue; public ZonedDateTime getTimestamp() { // is synchronisation

Synchronized vs ReadWriteLock performance

左心房为你撑大大i 提交于 2019-12-07 07:30:43
问题 I try to prove that synchronized is slower when there are many readers and only some writers. Somehow I proved the opposite. The RW example, time of execution is 313 ms: package zad3readWriteLockPerformance; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class Main { public static long start, end; public

B4. Concurrent JVM 锁机制(synchronized)

[亡魂溺海] 提交于 2019-12-07 07:08:32
B4. Concurrent JVM 锁机制(synchronized) https://www.cnblogs.com/zlxyt/p/11050346.html 挺好的 感觉这个文章写的 不过想要提高 还是得自己写代码 不写代码 肯定不行. 【 概述 】   JVM 通过 synchronized 关键字提供锁,用于在线程同步中保证线程安全。 【 synchronized 实现原理 】   synchronized 可以用于代码块或者方法中,产生同步代码区域,也叫互斥区。互斥区每次只能允许一个线程进入执行同步代码或重新进入执行剩余同步代码(参考线程进入等待状态后会唤醒,然后进入阻塞状态,重新获得锁的情况)。   synchronized 通过与一个对象进行绑定,或者说对一个对象进行加锁,并产生一个监控对象(monitor object)。如下图所示: 多线程并发执行同步代码,首先多个线程会进入集合(Entry Set),某个时间点只有一个线程可以获得(acquire) 监控对象的锁(monitor lock), 然后执行同步代码块。 如果获得锁的线程执行同步代码完毕,则会释放锁并退出同步代码区域(release and exit)。 若获得锁的线程在执行同步代码时调用被锁住对象的 wait 方法,该线程则会释放(release)锁,并进入集合(Wait Set)。锁被释放后

What is the difference in behavior between these two usages of synchronized on a list

时光毁灭记忆、已成空白 提交于 2019-12-07 02:08:59
问题 List<String> list = new ArrayList<String>(); list.add("a"); ... list.add("z"); synchronized(list) { Iterator<String> i = list.iterator(); while(i.hasNext()) { ... } } and List<String> list = new ArrayList<String>(); list.add("a"); ... list.add("z"); List<String> synchronizedList = Collections.synchronizedList(list); synchronized(synchronizedList) { Iterator<String> i = synchronizedList.iterator(); while(i.hasNext()) { ... } } Specifically, I'm not clear as to why synchronized is required in