多线程锁synchronized
作用:多线程中对共享数据操作时,需要加锁保证线程安全
锁的分类:
对象锁:synchronized加在普通方法上、同步语句块获取对象锁
类锁:synchronized加在静态方法上、同步语句块获取类锁
************************
示例
class Lock{
public synchronized void fun(){
System.out.println("开始执行fun方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("fun方法执行结束:"+System.currentTimeMillis()+"\n");
}
public synchronized void fun2(){
System.out.println("开始执行fun2方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("fun2方法执行结束:"+System.currentTimeMillis()+"\n");
}
}
class MyThread implements Runnable{
private boolean flag;
private Lock lock;
private CountDownLatch countDownLatch;
public MyThread(){
}
public MyThread(boolean flag,Lock lock,CountDownLatch countDownLatch){
this.flag=flag;
this.lock=lock;
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
if (flag){
lock.fun();
}else {
lock.fun2();
}
countDownLatch.countDown();
}
}
public class Test {
public static void main(String[] args){
Lock lock=new Lock();
CountDownLatch countDownLatch=new CountDownLatch(2);
MyThread thread=new MyThread(true,lock,countDownLatch);
MyThread thread2=new MyThread(false,lock,countDownLatch);
ExecutorService executorService= Executors.newFixedThreadPool(2);
executorService.submit(thread);
executorService.submit(thread2);
try {
countDownLatch.await();
}catch (Exception e){
e.printStackTrace();
}
executorService.shutdown();
}
}
*************************
控制台输出
开始执行fun方法:1621127025912
fun方法执行结束:1621127027925
开始执行fun2方法:1621127027925
fun2方法执行结束:1621127029937
fun、fun2同步执行:fun、fun2获取同一个对象锁
************************
示例 2
class Lock{
public synchronized static void fun(){
System.out.println("开始执行fun方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("fun方法执行结束:"+System.currentTimeMillis()+"\n");
}
public synchronized void fun2(){
System.out.println("开始执行fun2方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("fun2方法执行结束:"+System.currentTimeMillis()+"\n");
}
}
class MyThread implements Runnable{
private boolean flag;
private Lock lock;
private CountDownLatch countDownLatch;
public MyThread(){
}
public MyThread(boolean flag,Lock lock,CountDownLatch countDownLatch){
this.flag=flag;
this.lock=lock;
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
if (flag){
lock.fun();
}else {
lock.fun2();
}
countDownLatch.countDown();
}
}
public class Test2 {
public static void main(String[] args){
Lock lock=new Lock();
CountDownLatch countDownLatch=new CountDownLatch(2);
MyThread thread=new MyThread(true,lock,countDownLatch);
MyThread thread2=new MyThread(false,lock,countDownLatch);
ExecutorService executorService= Executors.newFixedThreadPool(2);
executorService.submit(thread);
executorService.submit(thread2);
try {
countDownLatch.await();
}catch (Exception e){
e.printStackTrace();
}
executorService.shutdown();
}
}
*************************
控制台输出
开始执行fun2方法:1621127668431
开始执行fun方法:1621127668431
fun2方法执行结束:1621127670445
fun方法执行结束:1621127670451
fun、fun2异步执行:fun获取类锁,fun2获取对象锁
************************
示例 3
class Parent{
public synchronized void fun(){
System.out.println("parent 开始执行fun方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("parent fun方法执行结束:"+System.currentTimeMillis()+"\n");
}
public synchronized void fun2(){
System.out.println("parent 开始执行fun2方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("parent fun2方法执行结束:"+System.currentTimeMillis()+"\n");
}
}
class Child extends Parent{
}
class MyThread2 implements Runnable{
private boolean flag;
private Child child;
private CountDownLatch countDownLatch;
public MyThread2(){
}
public MyThread2(boolean flag,Child child,CountDownLatch countDownLatch){
this.flag=flag;
this.child=child;
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
if (flag){
child.fun();
}else {
child.fun2();
}
countDownLatch.countDown();
}
}
public class Test3 {
public static void main(String[] args){
CountDownLatch countDownLatch=new CountDownLatch(2);
Child child=new Child();
MyThread2 myThread=new MyThread2(true,child,countDownLatch);
MyThread2 myThread2=new MyThread2(false,child,countDownLatch);
ExecutorService executorService=Executors.newFixedThreadPool(2);
executorService.submit(myThread);
executorService.submit(myThread2);
try {
countDownLatch.await();
}catch (Exception e){
e.printStackTrace();
}
executorService.shutdown();
}
}
*************************
控制台输出
parent 开始执行fun方法:1621128300064
parent fun方法执行结束:1621128302078
parent 开始执行fun2方法:1621128302079
parent fun2方法执行结束:1621128304082
子类对象fun、fun2同步执行:需要获取同一个对象锁child
************************
示例 4
class Parent{
public synchronized void fun(){
System.out.println("parent 开始执行fun方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("parent fun方法执行结束:"+System.currentTimeMillis()+"\n");
}
public synchronized void fun2(){
System.out.println("parent 开始执行fun2方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("parent fun2方法执行结束:"+System.currentTimeMillis()+"\n");
}
}
class Child extends Parent{
public void fun(){
System.out.println("child 开始执行fun方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("child fun方法执行结束:"+System.currentTimeMillis()+"\n");
}
public void fun2(){
System.out.println("child 开始执行fun2方法:"+System.currentTimeMillis());
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("child fun2方法执行结束:"+System.currentTimeMillis()+"\n");
}
}
class MyThread2 implements Runnable{
private boolean flag;
private Child child;
private CountDownLatch countDownLatch;
public MyThread2(){
}
public MyThread2(boolean flag,Child child,CountDownLatch countDownLatch){
this.flag=flag;
this.child=child;
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
if (flag){
child.fun();
}else {
child.fun2();
}
countDownLatch.countDown();
}
}
public class Test4 {
public static void main(String[] args){
CountDownLatch countDownLatch=new CountDownLatch(2);
Child child=new Child();
MyThread2 myThread=new MyThread2(true,child,countDownLatch);
MyThread2 myThread2=new MyThread2(false,child,countDownLatch);
ExecutorService executorService=Executors.newFixedThreadPool(2);
executorService.submit(myThread);
executorService.submit(myThread2);
try {
countDownLatch.await();
}catch (Exception e){
e.printStackTrace();
}
executorService.shutdown();
}
}
*************************
控制台输出
child 开始执行fun2方法:1621128483789
child 开始执行fun方法:1621128483789
child fun2方法执行结束:1621128485816
child fun方法执行结束:1621128485816
child fun、fun2异步执行:child重写了父类方法,方法上没有标识符synchronized