synchronized

java并发编程

吃可爱长大的小学妹 提交于 2019-12-03 05:54:22
1. ReentrantLock拥有与Synchronized相同的并发性和内存语义,此外还多了 锁投票,定时锁等候和中断锁等候。 线程A和B都要获取对象O的锁定,假设A获取了对象O锁,B将等待A释放对O的锁定, 如果使用 synchronized ,如果A不释放,B将一直等下去,不能被中断 如果 使用ReentrantLock,如果A不释放,可以使B在等待了足够长的时间以后,中断等待,而干别的事情 ReentrantLock获取锁定有三种方式: a) lock() , 如果获取了锁立即返回,如果别的线程持有锁,当前线程则一直处于休眠状态,直到获取锁 b) tryLock() , 如果获取了锁立即返回true,如果别的线程正持有锁,立即返回false; c) tryLock(long timeout, TimeUnit unit) , 如果获取了锁定立即返回true,如果别的线程正持有锁,会等待参数给定的时间,在等待的过程中,如果获取了锁定,就返回true,如果等待超时,返回false; d) lockInterruptibly :如果获取了锁定立即返回,如果没有获取锁定,当前线程处于休眠状态,直到或者锁定,或者当前线程被别的线程中断 2. synchronized是在JVM层面上实现的,不但可以通过一些监控工具监控synchronized的锁定,而且在代码执行时出现异常

Java SE1.6中的Synchronized

心不动则不痛 提交于 2019-12-03 04:47:15
1 引言 在多线程并发编程中Synchronized一直是元老级角色,很多人都会称呼它为重量级锁,但是随着Java SE1.6对Synchronized进行了各种优化之后,有些情况下它并不那么重了,本文详细介绍了Java SE1.6中为了减少获得锁和释放锁带来的性能消耗而引入的偏向锁和轻量级锁,以及锁的存储结构和升级过程。 2 术语定义 术语 英文 说明 CAS Compare and Swap 比较并设置。用于在硬件层面上提供原子性操作。在 Intel 处理器中,比较并交换通过指令cmpxchg实现。比较是否和给定的数值一致,如果一致则修改,不一致则不修改。 3 同步的基础 Java中的每一个对象都可以作为锁。 对于同步方法,锁是当前实例对象。 对于静态同步方法,锁是当前对象的Class对象。 对于同步方法块,锁是Synchonized括号里配置的对象。 当一个线程试图访问同步代码块时,它首先必须得到锁,退出或抛出异常时必须释放锁。那么锁存在哪里呢?锁里面会存储什么信息呢? 4 同步的原理 JVM规范规定JVM基于进入和退出Monitor对象来实现方法同步和代码块同步,但两者的实现细节不一样。代码块同步是使用monitorenter和monitorexit指令实现,而方法同步是使用另外一种方式实现的,细节在JVM规范里并没有详细说明,但是方法的同步同样可以使用这两个指令来实现

高并发编程基础知识

这一生的挚爱 提交于 2019-12-03 04:41:02
4)线程和进程的区别:(必考) 答: 进程是一个 “执行中的程序”,是系统进行资源分配和调度的一个独立单位; 线程是进程的一个实体,一个进程中拥有多个线程,线程之间共享地址空间和其它资源(所以通信和同步等操作线程比进程更加容易); 线程上下文的切换比进程上下文切换要快很多。 (1)进程切换时,涉及到当前进程的 CPU 环境的保存和新被调度运行进程的 CPU 环境的设置。 (2)线程切换仅需要保存和设置少量的寄存器内容,不涉及存储管理方面的操作。 面试官:进程间如何通讯?线程间如何通讯? 答:进程间通讯依靠 IPC 资源,例如管道(pipes)、套接字(sockets)等; 线程间通讯依靠 JVM 提供的 API,例如 wait()、notify()、notifyAll() 等方法,线程间还可以通过共享的主内存来进行值的传递。 什么是阻塞(Blocking)和非阻塞(Non-Blocking)? 答:阻塞和非阻塞通常用来形容多线程间的相互影响。比如一个线程占用了临界区资源,那么其他所有需要这个而资源的线程就必须在这个临界区中进行等待。等待会导致线程挂起,这种情况就是阻塞。此时,如果占用资源的线程一直不愿意释放资源,那么其他所有阻塞在这个临界区上的线程都不能工作。 非阻塞的意思与之相反,它强调没有一个线程可以妨碍其他线程执行。所有的线程都会尝试不断前向执行。 临界区是什么? 答

What does “synchronized” mean in Java? [duplicate]

匆匆过客 提交于 2019-12-03 04:38:12
This question already has an answer here: What does 'synchronized' mean? 15 answers I have been trying to learn design patterns. This site uses the synchronized keyword, but I don't understand what it does. I searched on the net and found that it is somewhat related to multi-threading and memory, but I am a mechanical engineer and don't understand what that means. Can anybody please help me understand threads and the synchronized keyword? There is no synchronized keyword in C++. There is one in Java, though, where for methods it means the following two things : It is not possible for two

用Java实现简单的线程死锁

强颜欢笑 提交于 2019-12-03 04:37:05
public class DeadLockDemo2 { public static void main(String[] args) { Object LockA=new Object(); Object LockB=new Object(); new Thread(new Runnable() { @Override public void run() { String name=Thread.currentThread().getName(); synchronized (LockA){ System.out.println(name+" get LockA want LockB"); try{ Thread.sleep(100); } catch (Exception e){ e.printStackTrace(); } synchronized (LockB){ System.out.println(name + " got lockB"); System.out.println(name + ": say Hello!"); } } } },"Thread-A").start(); new Thread(new Runnable() { @Override public void run() { String name=Thread.currentThread()

Does synchronized keyword prevent reordering in Java?

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Suppose I have the following code in Java a = 5; synchronized(lock){ b = 5; } c = 5; Does synchronized prevent reordering? There is no dependency between a, b and c. Would assignment to a first happen then to b and then to c? If I did not have synchronized, the statements can be reordered in any way the JVM chooses right? 回答1: Does synchronized prevent reordering? It prevents some re-ordering. You can still have re-ordering outside the synchronized block and inside the synchronized block, but not from inside a synchronized block, to outside

Volatile keyword in Java - Clarification [duplicate]

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Difference between volatile and synchronized in Java 4 answers I am really confused about what I read about the applications of volatile keyword in java. Is the following statement correct? "a write to a volatile field happens before every subsequent read of the same field" Ideally when should volatile keyword used? What is the difference between: class TestClass { private int x; synchronized int get(){return x;} synchronized void set(int x){this.x = x;} } and class TestClass { private volatile int x

How to use a std::mutex in a class context

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: i'm having some trouble using a C++11 std::mutex in my class I have a variable called semaphore of type std::mutex. So I positioned my semaphore.lock() and semaphore.unlock() before and after my critical section class Database { public: Database(string, string, string, string); virtual ~Database(); struct sMyHome getMyHome(void); struct sPhysical getPhysical(int); struct sSystemInfo getSystemInfo(void); void commitSystemInfo(struct sSystemInfo); struct sTSensors getTSensors(int); struct sWireless getWireless(int); struct sWirelessConf

Getting a thread to pause - Thread.wait()/Thread.notify()

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to understand how threads work, and I wrote a simple example where I want to create and start a new thread, the thread, display the numbers from 1 to 1000 in the main thread, resume the secondary thread, and display the numbers from 1 to 1000 in the secondary thread. When I leave out the Thread.wait()/Thread.notify() it behaves as expected, both threads display a few numbers at a time. When I add those functions in, for some reason the main thread's numbers are printed second instead of first. What am I doing wrong? public class