Как тут правильно реализовать синронизацию то? Чёт я запутался... помогите плиз, разъясните, как синхронизировать процесс на заводе???
| Код | public class Zavod {
/** * @param args */ public static void main(String[] args) { FirstMarka f_ceh=new FirstMarka(); SecondMarka s_ceh=new SecondMarka(); if (!f_ceh.sobratNewAvto(f_ceh)) System.out.println("Cann't create new auto. Please check Sklad."); else System.out.println("Succesfully create new auto. On first Ceh"); if (!s_ceh.sobratNewAvto(s_ceh)) System.out.println("Cann't create new auto. Please check Sklad."); else System.out.println("Succesfully create new auto. On second Ceh"); } }
public class FirstMarka extends Sklad { final private int need_dvizok=1; final private int need_kuzov=1; final private int need_pricep=0;
protected synchronized boolean sobratNewAvto(Sklad obj) { synchronized (obj){ synchronized (this){ if (checkDetails(obj)) { for (int i = 0; i < need_dvizok; i++) { if (!obj.getDetal(1)) { return false; } } for (int i = 0; i < need_kuzov; i++) { if (!obj.getDetal(2)) { return false; } } for (int i = 0; i < need_pricep; i++) { if (!obj.getDetal(3)) { return false; } } obj.addNewAuto(1); obj.SaveDataToFile(1, need_dvizok,need_kuzov,need_pricep); }else return false; }} return true; } private boolean checkDetails(Sklad obj) { int[] val = obj.getValues(); if (val[0]<need_dvizok || val[1]<need_kuzov || val[1]<need_pricep) return false; return true; } }
public class SecondMarka extends Sklad{ final private int need_dvizok=1; final private int need_kuzov=1; final private int need_pricep=1; protected synchronized boolean sobratNewAvto(Sklad obj) { synchronized (obj){ synchronized (this){ if (checkDetails(obj)) { for (int i = 0; i < need_dvizok; i++) { if (!obj.getDetal(1)) { return false; } } for (int i = 0; i < need_kuzov; i++) { if (!obj.getDetal(2)) { return false; } } for (int i = 0; i < need_pricep; i++) { if (!obj.getDetal(3)) { return false; } } obj.addNewAuto(2); obj.SaveDataToFile(2, need_dvizok,need_kuzov,need_pricep); }else return false; }} return true; } private boolean checkDetails(Sklad obj) { int[] val = obj.getValues(); if (val[0]<need_dvizok || val[1]<need_kuzov || val[1]<need_pricep) return false; return true; } }
import java.io.*;
public class Sklad { private int dvizok = 40; private int kuzov = 20; private int pricep = 30; private int first_count = 0; private int second_count = 1; protected synchronized void addNewAuto(int type) { switch (type) { case 1: synchronized (this){ first_count++; } break;
case 2: synchronized (this){ second_count++; } break; } } protected synchronized boolean getDetal(int type) { switch (type) { case 1: if ((dvizok-1)<1) return false; dvizok--; break;
case 2: if ((kuzov-1)<1) return false; kuzov--; break; case 3: if ((pricep-1)<1) return false; pricep--; break; } return true; } protected synchronized int[] getValues() { int[] values = {dvizok, kuzov, pricep}; return values; } protected void SaveDataToFile(int type, int need_dvizok, int need_kuzov, int need_pricep) { try { FileOutputStream outFile = new FileOutputStream ( "autozavod.txt", true) ; RandomAccessFile rf = new RandomAccessFile( "autozavod.txt", "rw"); DataOutputStream outData = new DataOutputStream ( outFile ) ; outData.writeUTF("\n"); outData.writeUTF ( "-------------------------------"+"\n" ) ; outData.writeUTF (" Ceh #"+type+": Successfully created auto \n"); outData.writeUTF ( "| Needed Dvig: "+ need_dvizok + "| \n"); outData.writeUTF ( "| Needed Kuzov: "+ need_kuzov +"| \n"); outData.writeUTF ( "| Needed Priceps: "+ need_pricep + "| \n"); outData.writeUTF ( "--------------------------------"+"\n" ) ; outData.writeUTF ( "Itogo na sklade: "+"\n" ) ; outData.writeUTF ( "| Dvig: "+ dvizok + "| \n"); outData.writeUTF ( "| Kuzov: "+ kuzov +"| \n"); outData.writeUTF ( "| Priceps: "+ pricep + "| \n"); outData.writeUTF ( "| First Auto: "+ first_count +"| \n"); outData.writeUTF ( "| Second Auto: "+ second_count + "| \n"); outData.writeUTF ( "--------------------------------"+"\n" ) ; outData.flush(); // outData.writeUTF ( favList [ i ] + "\n" ) ; //outData.writeDouble ( priceList [ i ] ) ; //outData.writeBytes("test"); // outData.writeChars("test"); // outData.writeUTF ( here ) ; outData.close ( ) ; rf.close(); }catch (IOException e) { System.out.println("ошибка файла: " + e); } } }
|
|