synchronized

do synchronized java methods queue calls?

亡梦爱人 提交于 2020-01-03 15:36:01
问题 I've read the oracle doc about synchronized methods and how they may introduce a lock to the multithreaded program, but there is one thing that is unclear to me. Are the subsequent calls to an already locked methods queued? Lets say we have a class: class Astore { ... public synchronized void a() { doSomethingTimeConsuming(); } ... } and 3 threads that call astore.a() final Astore astore = new Astore(); Thread t1 = new Thread(new Runnable() { public void run() { astore.a(); doSomethingElse();

Iterating over synchronized collection

妖精的绣舞 提交于 2020-01-03 08:18:10
问题 I asked here a question about iterating over a Vector , and I have been answered with some good solutions. But I read about another simpler way to do it. I would like to know if it is good solution. synchronized(mapItems) { Iterator<MapItem> iterator = mapItems.iterator(); while(iterator.hasNext()) iterator.next().draw(g); } mapItems is a synchronized collection: Vector. Is that make the iterating over the Vector safe from ConcurrentModificationException ? 回答1: Yes, it will make it safe from

synchronized关键字

懵懂的女人 提交于 2020-01-02 21:54:50
验证synchronized 是可重入锁? public class Xttblog extends SuperXttblog { public static void main(String[] args) { Xttblog child = new Xttblog(); child.doSomething(); } public synchronized void doSomething() { System.out.println( "child.doSomething()" + Thread.currentThread().getName()); doAnotherThing(); // 调用自己类中其他的synchronized方法 } private synchronized void doAnotherThing() { super .doSomething(); // 调用父类的synchronized方法 System.out.println( "child.doAnotherThing()" + Thread.currentThread().getName()); } } class SuperXttblog { public synchronized void doSomething() { System.out.println( "father

RecyclerView IndexOutOfBoundsException

最后都变了- 提交于 2020-01-02 03:32:25
问题 Why exception execute when I removed some items in RecyclerView by using loop ? I used Collentions.synchronizedMap in adapter and 'deleteItem method' use synchronized too (the method in fragment). public void elementController(JsonObject jsonObject , String type) { if ( jsonObject == null || type == null ) { return; } int position =0 , resultPosition =0; if ( type.equals("update") || type.equals("delete")) { String id = jsonObject.get(ELEMENT_ID).getAsString(); Map<String , Element> map =

线程协作---生产者消费者模式之“管程法”实现

ε祈祈猫儿з 提交于 2020-01-02 02:19:49
1 package cn.ftf.threadcooperation; 2 /** 3 * 协作模型:生产者消费者模式实现方式一:管程法,借助一个缓冲区 4 * @author 房廷飞 5 * 6 */ 7 8 public class CoTest01 { 9 public static void main(String[] args) { 10 SyContainer sy=new SyContainer(); 11 Productor pro=new Productor(sy); 12 pro.start(); 13 Consumer con = new Consumer(sy); 14 con.start(); 15 } 16 } 17 18 19 //生产者 20 class Productor extends Thread{ 21 SyContainer sycon; 22 public Productor(SyContainer sycon) { 23 super(); 24 this.sycon = sycon; 25 } 26 public void run() { 27 for(int i=0;i<100;i++) { 28 System.out.println("生产第"+i+"个馒头"); 29 sycon.push(new Streamedbun(i));

Multi Thread Java, but only one thread working

谁都会走 提交于 2020-01-01 19:37:28
问题 My Java Threads does not work independently, how to fix it? This is the initial main: Mechanics mechanics = new Mechanics(busShop, "Mechanic 1"); Mechanics mechanics2 = new Mechanics(busShop, "Mechanic 2"); Thread thMechanic = new Thread(mechanics); Thread thMehanic2 = new Thread(mechanics2); thMechanic.start(); thMehanic2.start(); No problem so far, work as expected, so the mechanics do this: public void run() { fixEngine(); } private void fixEngine() { while (true) { busShop.FixEngine

Multi Thread Java, but only one thread working

*爱你&永不变心* 提交于 2020-01-01 19:36:14
问题 My Java Threads does not work independently, how to fix it? This is the initial main: Mechanics mechanics = new Mechanics(busShop, "Mechanic 1"); Mechanics mechanics2 = new Mechanics(busShop, "Mechanic 2"); Thread thMechanic = new Thread(mechanics); Thread thMehanic2 = new Thread(mechanics2); thMechanic.start(); thMehanic2.start(); No problem so far, work as expected, so the mechanics do this: public void run() { fixEngine(); } private void fixEngine() { while (true) { busShop.FixEngine

多线程学习:Volatile与Synchronized的区别、什么是重排序

大城市里の小女人 提交于 2020-01-01 17:37:25
java线程的内存模型   java的线程内存模型中定义了每个线程都有一份自己的共享变量副本(本地内存),里面存放自己私有的数据,其他线程不能直接访问,而一些共享变量则存在主内存中,供所有线程访问。 上图中,如果线程A和线程B要进行通信,就要经过主内存,比如线程B要获取线程A修改后的共享变量的值,要经过下面两步: (1)、线程A修改自己的共享变量副本,并刷新到了主内存中。 (2)、线程B读取主内存中被A更新过的共享变量的值,同步到自己的共享变量副本中。 总结:在java内存模型中,共享变量存放在主内存中,每个线程都有自己的本地内存,当多个线程同时访问一个数据的时候,可能本地内存没有及时刷新到主内存,所以就会发生线程安全问题。 java多线程中的三个特性:   原子性: 即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行。一个很经典的例子就是银行账户转账问题:比如从账户A向账户B转1000元,那么必然包括2个操作:从账户A减去1000元,往账户B加上1000元。这2个操作必须要具备原子性才能保证不出现一些意外的问题。   可见性: 当多个线程访问同一个变量时,一个线程修改了这个变量的值,其他线程能够立即看得到修改的值。   有序性: 就是 程序执行的顺序按照代码的先后顺序执行。 一般来说处理器为了提高程序运行效率,可能会对输入代码进行优化

多线程(二)、内置锁 synchronized

谁都会走 提交于 2020-01-01 07:28:43
前言 在上一篇 多线程(一)、基础概念及notify()和wait()的使用 文章中我们讲了多线程的一些基础概念还有等待通知机制,在讲线程之间共享资源的时候,提到会出现数据不同步问题,我们先通过一个示例来演示这个问题。 /** * @author : EvanZch * description: **/ public class SynchronizedTest { // 赋count初始值为0 public static int count = 0 ; // 进行累加操作 public void add () { count++; } public static class TestThread extends Thread { private SynchronizedTest synchronizedTest; public TestThread (SynchronizedTest synchronizedTest) { this .synchronizedTest = synchronizedTest; } @Override public void run () { super .run(); // 执行10000次累加 for ( int x = 0 ; x < 10000 ; x++) { synchronizedTest.add(); } } } public int

Java Memory Model: reordering and concurrent locks

走远了吗. 提交于 2020-01-01 05:10:06
问题 The java meomry model mandates that synchronize blocks that synchronize on the same monitor enforce a before-after-realtion on the variables modified within those blocks. Example: // in thread A synchronized( lock ) { x = true; } // in thread B synchronized( lock ) { System.out.println( x ); } In this case it is garanteed that thread B will see x==true as long as thread A already passed that synchronized -block. Now I am in the process to rewrite lots of code to use the more flexible (and