java线程知识-同步

本文详细介绍了Java中四种线程同步的方法:synchronized关键字修饰方法、synchronized同步代码块、volatile关键字以及使用ReentrantLock重入锁。通过实例代码展示了它们在并发控制中的应用,确保线程安全。对于分布式环境,这些方法可能不再适用,需要采用分布式锁来解决。
摘要由CSDN通过智能技术生成

java线程同步的方式主要有

1、synchronized关键词 修饰方法

class bank {

public int take=100;//计数器

public  int getcount(){

return take;

}

public synchronized void Take(int i){   //同步关键词修饰方法

take=take-i;

System.out.println(" 拿了5块,还剩"+take);

}

//测试

public class testThread extends Thread{

private bank b;

public testThread(String s,bank b){

super(s);

this.b=b;

}

public  void run(){

for(int i=0;i<5;i++){

try{

Thread.sleep(1000);      //睡眠延迟时间     1000:大概延迟一秒

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.print(this.getName()+" :");

b.Take(5);

}

}

}

public static void main(String[] args) {

//启动线程

bank b1=new bank();

new testThread("tom",b1).start();

new testThread("joh",b1).start();

}

}

2、synchronized 同步代码块

class bank {

public int take=100;//计数器

public  int getcount(){

return take;

}

public  void Take(int i){   

take=take-i;

System.out.println(" 拿了5块,还剩"+take);

}

}

//测试

public class testThread extends Thread{

private bank b;

public testThread(String s,bank b){

super(s);

this.b=b;

}

public  void run(){

for(int i=0;i<5;i++){

try{

Thread.sleep(1000);      //睡眠延迟时间     1000:大概延迟一秒

}catch(InterruptedException e){

e.printStackTrace();

}

synchronized(this){      //同步代码块

System.out.print(this.getName()+" :");

b.Take(5);

}

}

}

public static void main(String[] args) {

//启动线程

bank b1=new bank();

new testThread("tom",b1).start();

new testThread("joh",b1).start();

}

}

3、使用特殊域变量volatile

class bank {

public volatile int take=100;//用volatile 修饰同步变量

public  int getcount(){

return take;

}

public void Take(int i){  

take=take-i;

System.out.println(" 拿了5块,还剩"+take);

}

}

//测试

public class testThread extends Thread{

private bank b;

public testThread(String s,bank b){

super(s);

this.b=b;

}

public  void run(){

for(int i=0;i<5;i++){

try{

Thread.sleep(1000);      //睡眠延迟时间     1000:大概延迟一秒

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.print(this.getName()+" :");

b.Take(5);

}

}

public static void main(String[] args) {

//启动线程

bank b1=new bank();

new testThread("tom",b1).start();

new testThread("joh",b1).start();

}

}

4、使用重入锁 :java.util.concurrent.ReentrantLock类

class bank {

public int take=100;

public  int getcount(){

return take;

}

private Lock lock=new ReentrantLock();//创建锁

public void Take(int i){   

lock.lock();       //上锁后 在解锁前的操作只能同步操作,不能异步运行

take=take-i;

System.out.println(" 拿了5块,还剩"+take);

lock.unlock();    //解锁

}

}

//测试

//导入相关包

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class testThread extends Thread{

private bank b;

public testThread(String s,bank b){

super(s);

this.b=b;

}

public  void run(){

for(int i=0;i<5;i++){

try{

Thread.sleep(1000);      //睡眠延迟时间     1000:大概延迟一秒

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.print(this.getName()+" :");

b.Take(5);

//	synchronized(this){

//	System.out.print(this.getName()+" :");

//	b.Take(5);

//	}

}

}

public static void main(String[] args) {

//启动线程

bank b1=new bank();

new testThread("tom",b1).start();

new testThread("joh",b1).start();

}

}

以上4种是针对单机应用的,如果是分布式的话,这些就不起效果了,需要用到分布式锁,后面会讲述到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值