I. Mission
Create 20 threads, where 10 threads are backing up data to the a database, and 10 threads backing up the data to the B database, and backing up the a database and backing up B databases is cross-running.
Second, the realization
1 . Implement the task of backing up a database and backing up B database.
/*** Description: When flag=true backup a database * When Flag=false backup B database to achieve cross-backup*/ Public classtaskbackup{volatile Private Booleanflag=false;//Use the volatile keyword to make the variable visible between multiple threads synchronized Public voidBackupa () {//synchronized keyword to prevent multiple threads from modifying the same object, causing "dirty reads" Try { //Remember, the judgment here must be used while instead of using if, why? Because there are multiple threads, the thread that is not just backing up the B database is waiting, and the thread that may be backing up the A database is waiting, if//may cause the same kind of wake-up situations that cause the thread to "feign death". while(flag==false){ This. Wait (); } System.out.println (Thread.CurrentThread (). GetName ()+ "Backing up A database!" ");//analog backup Database flag=false; This. Notifyall ();//waking up all the waiting threads, of course, does not wake up the Backupa thread here, because the Backupa thread is doing another while judgment, causing the thread to continue waiting, and only the BACKUPB thread is awakened.}Catch(interruptedexception e) {e.printstacktrace (); } } synchronized Public voidBACKUPB () {Try { while(flag==true){ This. Wait (); } System.out.println (Thread.CurrentThread (). GetName ()+ "Backing up B database!" ");Simulating a backup database
flag=truethiscatch (Interruptedexception e) { E.printstacktrace (); } } }
2. Create threads to perform backup tasks separately
public class Threadbackupa extends thread{ private Taskbackup Taskpackup; public Threadbackupa (taskbackup Taskpackup) { this . Taskpackup = Taskpa Ckup; } @Override public void run () { super .run (); Taskpackup.backupa (); }}
Public class extends thread{ private taskbackup taskpackup; Public THREADBACKUPB (taskbackup taskpackup) { this. Taskpackup= taskpackup; } @Override publicvoid run () { Super. Run (); TASKPACKUP.BACKUPB (); }}
3. Perform Tasks View results
Public class run{ publicstaticvoid main (string[] args) { taskbackup Taskpackup=new taskbackup (); for (int i=0;i<20;i++) { Threadbackupa threadbackupa=new Threadbackupa ( Taskpackup); THREADBACKUPB THREADBACKUPB=new THREADBACKUPB (taskpackup); Threadbackupa.start (); Threadbackupb.start (); }}}
Third, conclusion
I think this example is very good, so I made a note of it. It has made a good generalization application and solution to the problem of "suspended animation" such as the wait condition change in the process of thread notify and the same kind of wake-up. Pen Master qualifications is shallow, said bad place, also please feel free, thank you!
Wait/notify implementing multi-threaded cross-backup