synchronized

软开面试题合集--Binrry(冰蕊)

醉酒当歌 提交于 2019-12-24 13:39:27
线程和进程的区别 根本区别:进程是操作系统资源分配的基本单位,而线程是任务调度和执行的基本单位 在开销方面:每个进程都有独立的代码和数据空间(程序上下文),程序之间的切换会有较大的开销;线程可以看做轻量级的进程,同一类线程共享代码和数据空间,每个线程都有自己独立的运行栈和程序计数器(PC),线程之间切换的开销小。 所处环境:在操作系统中能同时运行多个进程(程序);而在同一个进程(程序)中有多个线程同时执行(通过CPU调度,在每个时间片中只有一个线程执行) 内存分配方面:系统在运行的时候会为每个进程分配不同的内存空间;而对线程而言,除了CPU外,系统不会为线程分配内存(线程所使用的资源来自其所属进程的资源),线程组之间只能共享资源。 包含关系:没有线程的进程可以看做是单线程的,如果一个进程内有多个线程,则执行过程不是一条线的,而是多条线(线程)共同完成的;线程是进程的一部分,所以线程也被称为轻权进程或者轻量级进程。 垃圾收集讲一下,1.8有哪些新特性 深入理解JVM垃圾收集机制(JDK1.8) JDK1.8 十大新特性详解 jvm调优有哪些参数 深入理解JVM-内存模型(jmm)和GC 深入详解JVM内存模型与JVM参数详细配置 tcp和udp,tcp网络架构 TCP与UDP的对比 TCP/IP架构 TCP/IP五层网络架构及OSI参考模型 nio与bio

java.lang.ClassCastException: Creating a synchronized Linked List

半腔热情 提交于 2019-12-24 13:00:21
问题 I want a synchronized LinkedList with a Generic Class A, here the relevant parts: public abstract class A<T extends B> implements Runnable { And the interface for B, where the "real" B, will be a supclass public interface B{ Following setup will throw a runtime error: LinkedList<A<? extends B>> eopList = (LinkedList<A<? extends B>>) Collections .synchronizedCollection(new LinkedList<A<? extends B>>()); I've found the following proposal, but it also won't compile: LinkedList<A<? extends B>>

java ,Properly using Static variables to prevent deadlock - Synchronizing

二次信任 提交于 2019-12-24 11:24:29
问题 I am new to android programming and 've not thoroughly studied java before . I am really confused on how to use synchronized to ensure thread safety on static variables : I usually create a class named Utils which has some static functions which are most of the time needed by all my other classes. Is this the correct way of keeping repetitive code out of my classes. I always suffer from a problem with using Databases. When I create sqliteHelper class for certain Database and try to manage

synchronize on different object visibility

主宰稳场 提交于 2019-12-24 09:24:45
问题 the following code shows synchronization on different object than this: public class A { int a,b,c,d; public void method1(Object x){ synchronized(x){ // is a ,b ,c ,d guarantee visibility ? } } public synchronized void method2() { a++; } } I know there will be a problem to edit a , b ,c ,d with having different lock in method1 and method2 , but the question is the changes flushed by method2 be visible to method1? because they don't use the same lock. 回答1: If you only read a , on x64 this will

java高级面试题总结

青春壹個敷衍的年華 提交于 2019-12-24 05:10:44
目录 1、java基础 1.1、hashmap原理?扩容 1.2、arraylist原理?扩容 1.3、jdk1.8新特性? 1.4、completablefuture 2、数据库 2.1、mysql索引优化 2.2、创建索引的依据? 2.3、mysql执行计划,explain各项参数代表什么意思? 2.4、什么时候不会用到索引? 2.5、mysql存储引擎 2.6、数据库隔离级别 2.7、为什么要使用索引 3、java多线程 3.1、java并发包java.util.concurrent及其子包都包括什么? 3.2、synconsized和volatile关键字区别? 3.3、实现线程池的方式? 3.4、公平锁与非公平锁 3.5、为什么不适用Excutors来创建线程池 3.6、ReentraneLock & AQS 4、jvm、java内存模型 4.1、jvm内存模型? 4.2、jvm调优具体调的那些参数? 5、java框架(spring boot,sprint cloud) 5.1、mybatis一级缓存,二级缓存 5.2、mybatis $ # 区别? 5.3、sprint cloud 的常用组件? 6、中间件 6.1、redis存储的数据类型 6.2、redis实现分布式锁原理,使用redis实现分布式锁有什么问题? 6.3、zookeeper实现分布式锁原理 6.4

Synchronized List/Map in Java if only one thread is writing to it

最后都变了- 提交于 2019-12-24 04:06:47
问题 The first thread is filling a collection continuously with objects. A second thread needs to iterate over these objects, but it will not change the collection. Currently I use Collection.synchronized for making it thread-safe, but is there a fast way to doing it? Update It's simple: The first thread (ui) continuously writes the mouse position to the ArrayList, as long as the mousebutton is pressed down. The second thread (render) draws a line based on the list. 回答1: Even if you synchronize

Lazy initialization of Singleton implementation without using Synchronized Key word

蹲街弑〆低调 提交于 2019-12-24 02:05:50
问题 I was asked in an interview to suggest a design/implementation of a Singleton Pattern where I have to Lazy load the class and also not use the synchronized key word. I got choked and could not come up with anything.I then I started reading on java concurrency and concurrentHaspMap. Please check the below imlpementation and kindly confirm if you see any issue with Double check Locking or any other issues with this implementation. package Singleton; import java.util.concurrent.ConcurrentHashMap

Should I acquire a lock on Properties first before looping setProperety?

余生颓废 提交于 2019-12-23 21:58:29
问题 The java class Properties is a thread-safe Class as per it's documentation: This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization. Because of that reason, I have the habit of maintaining my Properties in a HashMap implementation of Map , which is not thread-safe, but much more lightweight. More specifically, implementing thread-safety requires additional locking mechanisms, which will have an impact on performance. I can

java中String、StringBuffer和StringBuilder的区别

送分小仙女□ 提交于 2019-12-23 21:31:37
首先我们说一下java中String、StringBuffer和StringBuilder的相同点: 通过源码我们可以发现无论是String,StringBuffer还是StringBuilder它们的底层都是使用char[]数组进行存储。 接着我们说一下它们三者之间的不同点: String:不可变的字符序列。 StringBuffer:可变的字符序列,是线程安全的,效率低。 StringBuilder:可变的字符序列,是线程不安全的,效率高。 现在我们通过源码分析它们的不同点: 1.分析String的源码 /** The value is used for character storage. */ private final char value[]; //char[]数组被final修饰,所以它是不可变的字符序列。 2.分析StringBuffer的源码 char[] value; //在StringBuffer的父类中,没有用final修饰,是一个可变char[]数组 public StringBuffer() { super(16); //调用父类的构造器方法 } //父类的构造器 AbstractStringBuilder(int capacity) { value = new char[capacity]; //默认为我们创建了一个长度为16的char[]数组 } /

十八、线程同步、线程异步和锁机制synchronized

妖精的绣舞 提交于 2019-12-23 20:13:28
1、线程模型 1)线程异步:多个线程之间独立执行,谁也不用等谁 线程同步:一个线程必需等另外一个线程执行完毕之后才能执行 备注:为了数据的安全,尽管应用程序的使用率低,但是为了保证数据是安全的,必须加入线程同步机制,线程同步机制使程序变成了(等同)单线程 2)什么条件下要使用线程同步? 第一: 必须是多线程环境 第二: 多线程环境共享同一个数据 第三: 共享的数据涉及到修改操作 3) 同步方法和同步代码块的区别 : 在 Java 语言中,每一个对象有一把锁。线程可以使用 synchronized 关键字来获取对象上的锁。synchronized 关键字可应用在方法级别(粗粒度锁)或者是代码块级别(细粒度锁) 2、synchronized 锁的一些概念: 1) java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁称为内置锁。 线程进入同步代码块或方法的时候会自动获得该锁 ,在退出同步代码块或方法时会释放该锁。获得内置锁的唯一途径就是进入这个锁的保护的同步代码块或方法 java内置锁是一个互斥锁,这就是意味着最多只有一个线程能够获得该锁,当线程A尝试去获得线程B持有的内置锁时,线程A必须等待或者阻塞,直到线程B释放这个锁,如果B线程不释放这个锁,那么A线程将永远等待下去 java的对象锁和类锁:java的对象锁和类锁在锁的概念上基本上和内置锁是一致的,但是