synchronized

Synchronizing on an object and changing the reference

可紊 提交于 2019-12-10 21:37:27
问题 Let's say I have an object as follows: Map<String, String> m = new HashMap<>(); Then I synchronize on this object as follows and change its reference: synchronize(m){ m = new HashMap<>(); } With this code, what happens to the lock on m? Is it still safe to update the new object represented by m? Or is the lock essentially on the old object? 回答1: From JLS 17.1: The synchronized statement (§14.19) computes a reference to an object; it then attempts to perform a lock action on that object's

Is it useful to have a dedicated object for a lock? [duplicate]

前提是你 提交于 2019-12-10 21:21:05
问题 This question already has answers here : What is the difference between synchronized on lockObject and using this as the lock? (5 answers) Closed last year . I am cleaning up a legacy java code and I found following construct: final class QuiteComplexClass { private Object lock = new Object(); void aMethod() { ... synchronized(lock) { .... } } } Is the special object for locking necessary here? What is difference when I used simple synchronized (this) ? I think it might by useful when the

Multithreading--Why one thread is doing all of the work?

五迷三道 提交于 2019-12-10 19:04:02
问题 I am multiplying two matrices using two threads (however, the program is written to scale up as well, so I could possibly use three, four, etc threads instead). Each thread calculates/does the work for one row (or column) of the final matrix. If one thread is doing work on a row, the other one(s) should not work on that row. It/they should move on to the next available row. First of all, I am not certain if the way I implemented the problem is correct. If you can see a better way, please let

when we use synchronized keyword,what gets locked? [duplicate]

有些话、适合烂在心里 提交于 2019-12-10 17:49:00
问题 This question already has answers here : Java synchronized method lock on object, or method? (11 answers) Closed 5 years ago . Question comes to my mind while reading about the concurrency issues in threads and dealing with it through synchronized keyword is that,when we use the term lock it is used for the object which contains the run method (or the job of the thread).But why we cannot use the term lock for the method only whose definition contains the synchronized keyword,as this keyword

并发编程(五)线程同步

我是研究僧i 提交于 2019-12-10 17:28:25
一、 引言   多线程的开发过程中,也许会遇到这么一个场景:多个线程同时操作一个变量时,线程之间会有时间差,而在时间差内,该共享数据的值也许已经发生了改变,那么我们要怎么才能保证在多线程的环境下,每个线程读取到的数据值都是最新的呢?线程同步机制了解一下~ 二、 线程同步的“锁”   前面了解了多线程场景下,需要保证每个线程读取数据的值都要是最新的,那么我们就需要一个“锁”,来保证当前线程操作此数据时,别的线程无法使用,相当于当前线程“锁”住了此时数据的读写权限,下面我们来学习下如何实现这一场景。   首先我们先了解几大神器,以及其大致的作用: synchronized : 关键字 ,可以修饰方法,也可以修饰代码块 volatile : 特殊域变量 ReentrantLock : 重入锁 Atomic : 原子变量 三、 线程同步详解 synchronized同步方法    定义: 有synchronized关键字修饰的方法。 每个java对象都有一个 内置锁 ,使用synchronized关键字修饰方法时, 内置锁就会“锁住”整个方法。 每个线程在调用该方法前,都需要获得内置锁,否则就处于阻塞状态。 PS: 若用synchronized关键字修饰 静态方法 ,此时如果调用该静态方法,将 会 锁住整个类 /** * synchronized同步方法 */ public class

方法Join()

江枫思渺然 提交于 2019-12-10 17:16:18
等待线程结束:jion() 谦让: yeild() 睡眠: sleep() jion和sleep和yeild之间有什么区别呢? 首先介绍一下jion(): 如果想让主线程等待子线程执行完毕之后再继续运行,比如线程处理一个数据,主线程想要获得这个线程的处理结果,因为线程之间是并行的,这个时候就需要用到jion()方法。方向jion()的作用就是等待线程对象销毁。 先来看一段示例代码: public class MyThread extends Thread { @Override public void run(){ try{ int value = (int) (Math.random()*1000); System.out.println("i will sleep for "+value+" ms"); Thread.sleep(value); }catch (InterruptedException e){ e.printStackTrace(); } } } public class Test { public static void main(String[] args) { try{ MyThread thread = new MyThread(); thread.start(); thread.join(); System.out.println(

Do I need to implement synchronized on writing data to a same file by using BufferedWriter and FileWriter?

冷暖自知 提交于 2019-12-10 15:09:23
问题 I am working on Webmethods Integration Server. Inside there is a java service which is in form of a static java method for writing data to a log file (server.log) by using BufferedWriter and FileWriter. The static method code is like this: public static void writeLogFile(String message) throws ServiceException{ try { BufferedWriter bw = new BufferedWriter(new FileWriter("./logs/server.log", true)); bw.write(message); bw.newLine(); bw.close(); } catch (Exception e) { throw new ServiceException

FileReader API: how to read files synchronously

◇◆丶佛笑我妖孽 提交于 2019-12-10 14:18:57
问题 I am trying to read a file which is selected using an input type file on the html page. I have implemented the function to read the file and the file content is able to be read. But the actual problem is that the reading of the content of the file is being done asynchronously which allows the other functions of the script to execute. I am storing the content of the file read in an array. While moving to the other functions the array is empty. When delay is introduced then the array has the

BluetoothChat synchronized onResume Activity lifecycle method, why?

孤街醉人 提交于 2019-12-10 14:16:36
问题 I'm studying right now the bluetooth Android API, and I ran into the BluetoothChat example. http://developer.android.com/resources/samples/BluetoothChat/index.html It contains many errors, first of all the simple fact that it uses API 11 but manifest does not force this minimum API. Other interesting thing is the use of synchronized keyword on Activity lifecycle methods, like on onResume: @Override public synchronized void onResume() { super.onResume(); if(D) Log.e(TAG, "+ ON RESUME +"); //

Java Create Two Threads but Only One Running

安稳与你 提交于 2019-12-10 12:24:46
问题 I might have asked earlier here, and there is lack of understanding and complex, Now I rewrite the program in a much easier way to understand. The Problem: When I run the 2 threads, only 1 thread do the job. Suspicion Helper I suspect that the thread lock itself, so that another thread cannot access it. Code Initialization Main Shop shop = new Shop(); CarGenerator carGenerator = new CarGenerator(shop); Mechanics mechanics1 = new Mechanics(shop, "Mechanics 1"); Mechanics mechanics2 = new