![]() | 1 2 3 4 5 6 7 8 Ім'я файлу: пз.docx Розширення: docx Розмір: 1133кб. Дата: 26.05.2023 скачати Пов'язані файли: 16. 04. лекція Тригери на БТ.docx Методичні вказівки до практичних занять з дисципліни «Комп’ютерн Методичні вказівки до практичних занять з дисципліни «Комп’ютерн import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Serializable; import java.io.StringReader; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; import java.util.concurrent.Callable; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import javax.print.DocFlavor.INPUT_STREAM; import javax.print.attribute.standard.Chromaticity; public class Mthread Callable T res; static ServerSocket rout; String classpath=null; public Mthread(Callable target=ttarget; this.classpath=classpath; try { rout = new ServerSocket(1007); } catch (Exception e) { System.out.println("EXCEPTION in construct"); } } public T call() throws Exception { File resf=null; Class targ = target.getClass(); if (classpath!=null) { resf=new File(classpath); System.out.println("not null classpath");} else { String[] path = (System.getenv("PATH")+":./").split(":"); for (String item : path) { File f = new File(item+"/"+targ.getName().replace(".", "/")+".class"); if (f.exists()) { resf=f; break; } } } if (resf==null) return null; Socket s=null; ObjectOutputStream oos=null; try { System.out.println("created"); s = new Socket("127.0.0.1",8000); oos= new ObjectOutputStream(s.getOutputStream()); oos.writeObject(target.getClass().getName()); long filelength = resf.length(); System.out.println(filelength); BufferedInputStream bis =new BufferedInputStream(new FileInputStream(resf)); int bbb=100; byte[] buf =new byte[100]; long currentlen=0; long length=0; while (currentlen if ((length=filelength-currentlen) buf= new byte[(int)length]; } length=bis.read(buf); oos.write(buf, 0, (int)length); oos.flush(); currentlen+=length; } bis.close(); } catch (IOException e) { System.out.println("exception"); e.printStackTrace(); } System.out.println(target.getClass().getName()); //th2.start(); ObjectInputStream in = new ObjectInputStream(s.getInputStream()); PrintBack pb = new PrintBack(in); oos.writeObject(target); //ExecutorService exec = Executors.newCachedThreadPool(); boolean exit=true; res =(T) in.readObject(); in.close(); oos.close(); s.close(); rout.close(); return res; } } class PrintBack implements Runnable { ObjectInputStream ois; PrintBack(ObjectInputStream ois) throws IOException { this.ois=ois; Thread th = new Thread(this); th.start(); } @Override public void run() { try { } catch (Exception e) { //System.out.println("EXCEPTION in run"); } } } package balancer; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.Set; public class Balansing { public static ArrayList private static Balansing B=null; public static String curip=null; public static Map public static Balansing getInstance() { if (B==null) { B = new Balansing(); } return B; } private Balansing() { map= new HashMap } public synchronized void del(String ip) { map.remove(ip); ips.remove(ip); balance(); } public synchronized void update(String ip, Double cpu) { if (map.containsKey(ip)) { map.remove(ip); ips.remove(ip); } map.put(ip, cpu); ips.add(ip); } public synchronized void balance() { String ip=""; if (map.size()==0) { System.out.println("size map = 0"); return;} Set Double min = 0.0; for (String item : s) { Double cur = map.get(item); if (cur>min) { min=cur; ip=item; } } curip=ip; System.out.println(curip); } public synchronized String getIP() { return ips.get(); } } 1 2 3 4 5 6 7 8 |