文章目录
Java进阶篇 玩转synchronized
相信通过前面的线程终止三大方法、线程调度、线程安全(同步锁synchronized)讲解,大家对synchronized有了一个初步的了解,本篇就主要讲解一下,在面试中synchronized的一些要点
synchronized出现在实例方法上
synchronized出现在实例方法上,一定锁的是this。没得挑。只能是this,不能是其他对象
所以这种方法是不灵活
另外还有一个缺点
这样用或者会提高同步范围,降低效率。
优点:
。。。代码写的少了,节俭了
如果共享的对象是this,并且需要同步整个方法体 建议使用这种方式
就比如:我们常用的一些集合里边(线程安全的都是在实例方法上加了 synchronized关键字,可以自行搜索源码查看)
- ArraysList 是非线程安全的
- Vector 是线程安全的
- HashMap HashSet是非线程安全的
- Hashtable 是线程安全的
synchronized面试题1
package com.zhou.exam1;
/**
* 面试题 doOther方法的执行要等到doSome方法结束吗?
*/
public class exam01 {
public static void main(String[] args) throws InterruptedException {
Myclass mc = new Myclass();
Thread t1 = new Mythread1(mc);
Thread t2 = new Mythread1(mc);
t1.setName("t1");;
t2.setName("t2");
t1.start();
Thread.sleep(1000);
t2.start();
}
}
class Mythread1 extends Thread{
private Myclass mc;
public Mythread1(Myclass mc)
{
this.mc = mc;
}
@Override
public void run() {
if (Thread.currentThread().getName().equals("t1")) mc.doSome();
if (Thread.currentThread().getName().equals("t2")) mc.doOther();
}
}
class Myclass{
public synchronized void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public void doOther(){
System.out.println("doOther begin");
System.out.println("doOther over");
}
}
synchronized面试题2
在doOther方法上加上synchronized之后呢结果又是如何
package com.zhou.exam2;
/**
* 面试题 doOther方法的执行要等到doSome方法结束吗
*/
public class exam01 {
public static void main(String[] args) throws InterruptedException {
Myclass mc = new Myclass();
Thread t1 = new Mythread1(mc);
Thread t2 = new Mythread1(mc);
t1.setName("t1");;
t2.setName("t2");
t1.start();
Thread.sleep(1000);
t2.start();
}
}
class Mythread1 extends Thread{
private Myclass mc;
public Mythread1(Myclass mc)
{
this.mc = mc;
}
@Override
public void run() {
if (Thread.currentThread().getName().equals("t1")) mc.doSome();
if (Thread.currentThread().getName().equals("t2")) mc.doOther();
}
}
class Myclass{
public synchronized void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public synchronized void doOther(){
System.out.println("doOther begin");
System.out.println("doOther over");
}
}
synchronized面试题3
如果定义线程的时候传入的对象不一样的话,结果又是如何呢?
package com.zhou.exam3;
/**
* 面试题 doOther方法的执行要等到doSome方法结束吗
*/
public class exam01 {
public static void main(String[] args) throws InterruptedException {
Myclass mc1 = new Myclass();
Myclass mc2 = new Myclass();
Thread t1 = new Mythread1(mc1);
Thread t2 = new Mythread1(mc2);
t1.setName("t1");;
t2.setName("t2");
t1.start();
Thread.sleep(1000);
t2.start();
}
}
class Mythread1 extends Thread{
private Myclass mc;
public Mythread1(Myclass mc)
{
this.mc = mc;
}
@Override
public void run() {
if (Thread.currentThread().getName().equals("t1")) mc.doSome();
if (Thread.currentThread().getName().equals("t2")) mc.doOther();
}
}
class Myclass{
public synchronized void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public synchronized void doOther(){
System.out.println("doOther begin");
System.out.println("doOther over");
}
}
synchronized面试题4
如果把两个方法都设置成静态方法结果又是什么呢?
package com.zhou.exam4;
/**
* 面试题 doOther方法的执行要等到doSome方法结束吗
*/
public class exam01 {
public static void main(String[] args) throws InterruptedException {
Myclass mc1 = new Myclass();
Myclass mc2 = new Myclass();
Thread t1 = new Mythread1(mc1);
Thread t2 = new Mythread1(mc2);
t1.setName("t1");;
t2.setName("t2");
t1.start();
Thread.sleep(1000);
t2.start();
}
}
class Mythread1 extends Thread{
private Myclass mc;
public Mythread1(Myclass mc)
{
this.mc = mc;
}
@Override
public void run() {
if (Thread.currentThread().getName().equals("t1")) mc.doSome();
if (Thread.currentThread().getName().equals("t2")) mc.doOther();
}
}
class Myclass{
//synchronized出现在静态方法上是找类锁
public synchronized static void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public synchronized static void doOther(){
System.out.println("doOther begin");
System.out.println("doOther over");
}
}
大家可以自行测验哦,小编就不给答案了。
总结
synchronized 总结下来如下
在线程调用被此关键字修饰的代码块时有如下特点
- 此线程会进入锁池,寻找synchronized(共享对象)一般为实例变量(堆区)和静态变量(方法区) 如果未找到,此线程就会在锁池中进行等待,直到其他线程把共享对象释放为止。
- 修饰在类方法上共享对象默认为this也就是当前对象的对象锁
- 静态修饰在方法上共享对象为当前类的类锁