java 线程 (中) :常见的线程同步方式

2025-11-08 07:15:06

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();

}

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:149
  • 阅读量:170
  • 阅读量:146
  • 阅读量:49
  • 阅读量:62
  • 猜你喜欢