JavaSynchronized修饰不一样的参数.

刚开始学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

猜你喜欢

转载自blog.csdn.net/weixin_43299461/article/details/86562904
今日推荐