java Snake Skin Multithreading (Final Chapter)

Keywords: Attribute

Multithreaded synchronization


Take a bank for example

public class ThreadTest13 {

	public static void main(String[] args) {
		Account account=new Account("Call me God O(∩_∩)O",5000);
		Thread t1=new Thread(new Processor(account));
		Thread t2=new Thread(new Processor(account));
		t1.start();
		t2.start();
	}

}
//Threads for withdrawal
class Processor implements Runnable{
	//account
	Account act;
	Processor(Account act){
		this.act=act;
	}
	
	public void run() {
		act.withdraw(1000);
		System.out.println(act.getActno()+"Successful withdrawal of 1000 pieces, balance:"+act.getbalance());
		
	}
}
//account
class Account{
	//attribute
	private String actno;
	private double balance;
	//Construction method
	public Account() {}
	public Account(String actno,double balance) {
		this.setActno(actno);
		this.setbalance(balance);
	
	}
	//set and get
	public String getActno() {
		return actno;
	}
	public void setActno(String actno) {
		this.actno=actno;
	}
	public double getbalance() {
		return balance;
		
	}
	public void setbalance(double balance) {
		this.balance=balance;
		
	}
	//Provide a method of withdrawal from the outside world
	public synchronized void withdraw(double money) {
		synchronized (this) {
			//Put the code to be synchronized in the synchronization statement block
			/*
			 * Principles t1 and t2
			 * t1 When a thread reaches this point, it encounters the keyword synchronized and goes back to find the this object lock
			 *If a this object lock is found, it enters the synchronization statement block to execute the program. When code execution ends in the synchronization statement block, the t1 thread returns the this object lock.
			 *
			 * During the t1 thread executing the synchronization statement block, if the t2 thread also comes to execute the following program, it will also encounter the synchronzed keyword, so it will also look for the this object lock, which is held by the t1 thread and can only wait here for the return of the this object lock
			 */
			double after=balance-money;
			//To update
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
			this.setbalance(after);
			
		}
	}
	
}


Small aspects of synchronized



When t1 shares a resource with t2 (threads of the same process share * memory and resources of their process *

Shared memory is * stack memory and method memory * stack memory is not shared, each thread has its own stack space (one thread per stack)

public class ThreadTest14 {

	public static void main(String[] args) throws InterruptedException {
		// TODO auto-generated method stub
		MyClass mc =new MyClass();
		F a=new F(mc);//t1 and t2 Share one mc
		Thread t1=new Thread(a);
		t1.setName("t1");
		Thread t2=new Thread(a);
		t2.setName("t2");
		t1.start();
		//Ensure t1 executes first
		Thread.sleep(1000);
		t2.start();
		
	}

}
class F implements Runnable{
	MyClass mc;
	F(MyClass mc){
		this.mc=mc;
	}
	public void run() {
		if (Thread.currentThread().getName().equals("t1"))
			mc.m1();
		if(Thread.currentThread().getName().equals("t2"))
			mc.m2();
	}
}
class MyClass{
	public synchronized void m1() {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		System.out.println("m1...");
	}
//	Public void m2() {M2 does not need to wait for m1 to end M2 without synchronized keyword
//		System.out.println("m2....");
//	}
	public synchronized void m2(){//The m2 method waits for m1 to end t1 and t2 to share an mc, and both m1 and m2 have synchronized
	System.out.println("m2......");
}
}
For the first time, m2 did not add the synchronized keyword (not share a pit with m1)



The second m2 added the synchronized keyword (share a pit with m1)


Object Lock

Posted by r3drain on Tue, 14 Jul 2020 09:18:05 -0700