JavaSynchronized修饰不一样的参数.

本文通过示例深入解析Java多线程中同步的概念,包括如何使用synchronized关键字实现线程安全,以及通过具体代码展示同步在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚开始学java的时候,就最先学习多线程,那时候云里雾里不懂什么叫同步,现在也算是了却了心结.

修饰代码块:

class T implements Runnable {
	private static int count;

	public T() {
		count = 0;
	}

	public void add() {
		synchronized (this) {
			for(int i=0;i<5;i++) {
			count++;
			System.out.println(Thread.currentThread().getName() + "....." + count);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		}
	}

	public void read() {
		for(int i=0;i<5;i++) {
		System.out.println(Thread.currentThread().getName() + "....." + count);
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}

	public void run() {
		
			String threadName=Thread.currentThread().getName();
			if(threadName.equals("A")) {
				add();
			}else {
				read();
			}
		
	}
}

public class SynchronizedDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		T t = new T();
		Thread t1 = new Thread(t, "A");
		Thread t2 = new Thread(t, "B");
		t1.start();
		t2.start();
	}

}

在这里插入图片描述
修饰一个对象

class Account{
	String name;
	float amount=0;
	public Account(String name) {
		this.name=name;
	}
	
	public void saveMoney(float amt) {
		amount+=amt;
		
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void withdrawMoney(float amt) {
		amount-=amt;
		
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void showMoney(){
		System.out.println(Thread.currentThread().getName()+name+"当前余额"+amount+"元");
	}
}
class AccountOperator implements Runnable{
	private Account acco;
	
	public AccountOperator(String Accountname) {
		// TODO Auto-generated constructor stub
		this.acco=new Account(Accountname);
	}
	public void run() {
		// TODO Auto-generated method stub
		 synchronized(acco) {
			 acco.showMoney();
			 acco.saveMoney(500);
			 acco.showMoney();
			 acco.withdrawMoney(100);
			 acco.showMoney();
			
		 }
	}
	
}
public class SynchronizedDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AccountOperator accoter=new AccountOperator("Fjy");
		Thread t1=new Thread(accoter);
		t1.start();
		Thread t2=new Thread(accoter);
		t2.start();
		
	}

}

在这里插入图片描述

想让代码同步运行:

class Syn implements Runnable{

	private void a() {
		for(int i=0;i<10;i++) {
			System.out.println("aaaa");
		}
	}
	private void b() {
		for(int i=0;i<10;i++) {
			System.out.println("bbbb");
		}
	}
	
	private String s=new String();
	public void run() {
		// TODO Auto-generated method stub
		synchronized(s) {
			a();
			b();
		}
	}
	
}
public class SynchronizedDemo3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Syn syn=new Syn();
		Thread t1=new Thread(syn);
		t1.start();
		Thread t2=new Thread(syn);
		t2.start();
	}

}

在这里插入图片描述

详细:http://www.importnew.com/21866.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值