java高并发之核心synchronized

本文详细探讨了Java中的synchronized关键字在实现线程同步中的两种用法:对象锁和类锁。通过多个示例展示了同步方法和同步代码块的使用,解释了它们在多线程环境中的行为,包括不同线程访问同步和非同步方法的情况,以及异常处理中锁的释放。文章旨在帮助读者深入理解Java并发编程的核心概念。
摘要由CSDN通过智能技术生成

一.synchronized的两个用法

①对象锁

方法锁(sychronized修饰普通方法,默认锁对象为this当前对象)和同步代码块锁(自己指定锁对象)

eg1

public class synchronizedobjectcodeblock2 implements runnable{

static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();
@override
public void run() {
synchronized (this) {
System.out.printin(" I am object lock block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);

} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"运行结束");
}}


public static void main(String[] args) {

Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");

}}

eg2

public class synchronizedobjectcodeblock2 implements runnable{

static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();

Object  lock=new  Object();
@override
public void run() {
synchronized (lock) {
System.out.printin(" I am object lock block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);

} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"运行结束");
}}


public static void main(String[] args) {

Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");

}}

eg3

public class synchronizedobjectcodeblock2 implements runnable{

static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();

Object  lock1=new  Object();

Object  lock2=new  Object();
@override
public void run() {
synchronized (lock1) {
System.out.printin(" I am object lock1 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);

} catch (InterruptedException e)

 {e.interruptedexception trace ();

}
System.out.println (Thread.currentthread ().getname()+"lock1运行结束");
}

synchronized (lock2) {
System.out.printin(" I am object lock2 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);

} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"lock2运行结束");
}

}


public static void main(String[] args) {

Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");

}}

eg4

public class synchronizedobjectcodeblock2 implements runnable{

static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();

Object  lock1=new  Object();

Object  lock2=new  Object();
@override
public void run() {
synchronized (lock1) {
System.out.printin(" I am object lock1 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);

} catch (InterruptedException e)

 {e.interruptedexception trace ();

}
System.out.println (Thread.currentthread ().getname()+"lock1运行结束");
}

synchronized (lock1) {
System.out.printin(" I am object lock2 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);

} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"lock2运行结束");
}

}


public static void main(String[] args) {

Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");

}}

 

 

eg5

public class synchronizedobjectcodeblock3 implements runnable{

static synchronized0bjectcodeblock3 instance = newsynchronized0bjectcodeb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值