This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Adresáre rekurzívne class FilterAdresara implements FilenameFilter { public boolean accept(File dir, String name) { File f = new File(dir, name); return f.isDirectory(); }}public class PodadresareRek { static void rekVypis(String aktualnyAdr) { String[] mena; File aktDir = new File(aktualnyAdr); FilterAdresara FilterAdr = new FilterAdresara(); mena = aktDir.list(FilterAdr); if (mena != null) { for (int i = 0; i < mena.length; i++) { String podadr = new String (aktualnyAdr + "\\" + mena[i]); System.out.println(podadr); rekVypis(podadr); } }
rekVypis(System.getProperty("user.dir"));
SerializáciaObjectOutputStream
FileOutputStream out = new FileOutputStream("AVL");
ObjectOutputStream fs = new ObjectOutputStream(out);
fs.writeObject("avl"); // String
fs.writeObject(s); // AVLTree
fs.flush();
ObjectInputStream
FileInputStream in = new FileInputStream("AVL");
ObjectInputStream is = new ObjectInputStream(in);
String str = (String)is.readObject();
AVLTree ss = (AVLTree)is.readObject();
Serializable Interface class AVLNode implements Serializableclass AVLTree implements Serializable
AVL:¬í t avlsr AVLTree¡6ûG‰> L roott LAVLNode;xpsr AVLNodeÙ=œö½â I xL leftq ~ L rightq ~ xp ssq ~ /sq ~ sq ~ ppsq ~ ppsq ~ Qsq ~ @ppsq ~ bppsq ~ •sq ~ „ppsq ~ ¦psq ~ ·pp
public class SimplePriorities extends Thread { private int countDown = 5; private volatile double d = 0; // No optimization public SimplePriorities(int priority) { setPriority(priority); start(); } public void run() { while(true) { for(int i = 1; i < 100000; i++) d = d + (Math.PI + Math.E) / (double)i; System.out.println(this); if(--countDown == 0) return; } } public static void main(String[] args) { new SimplePriorities(Thread.MAX_PRIORITY); for(int i = 0; i < 5; i++) new SimplePriorities(Thread.MIN_PRIORITY); }}
Sleeper prvy = new Sleeper("Prvy", 1500); Sleeper druhy = new Sleeper("Druhy", 1500), Joiner treti = new Joiner("Treti", druhy), Joiner stvrty = new Joiner("Strvrty", prvy); prvy.interrupt();
Prvy was interrupted. isInterrupted(): false
Strvrty completed
Druhy has awakened
Treti completed
Rúryclass Sender extends Thread { private Random rand = new Random(); private PipedWriter out = new PipedWriter(); public PipedWriter getPipedWriter() { return out; } public void run() { while(true) { for(char c = 'A'; c <= 'z'; c++) { try { out.write(c); sleep(rand.nextInt(500)); } catch(Exception e) { throw new RuntimeException(e); } } } }}
class Receiver extends Thread { private PipedReader in; public Receiver(Sender sender) throws IOException { in = new PipedReader(sender.getPipedWriter()); } public void run() { try { while(true) { System.out.println("Read: " + (char)in.read()); } } catch(IOException e) { throw new RuntimeException(e); } }}
public class PipedIO { public static void main(String[] args) throws Exception { Sender sender = new Sender(); Receiver receiver = new Receiver(sender); sender.start(); receiver.start(); new Timeout(4000, "Terminated"); }}
Guličky - threadypublic class Gulicka extends Thread { public int x = ..., y = …, dx = …, dy = … public void run() { while (true) { x += dx; y += dy; if (x >= sizeX) { x = sizeX - (x-sizeX); dx = -dx; } else if (y >= sizeY) { y = sizeY - (y-sizeY); dy = -dy; } else if (x < 0) { x = -x; dx = -dx; } else if (y < 0) { y = -y; dy = -dy; } ap.repaint(); try { sleep(10); } catch(Exception e) {} } }}
public class Sapplet extends Applet {SortPanel c1, c2, c3, c4; public void init() { setSize(800,200); setLayout(new GridLayout(1,4)); c1 = new SortPanel("Buble",Color.MAGENTA); add(c1); c2 = new SortPanel("Quick",Color.BLUE); add(c2); c3 = new SortPanel("Merge",Color.RED); add(c3); c4 = new SortPanel("Random",Color.GREEN); add(c4); } public void start() { c1.start(); c2.start(); c3.start(); c4.start(); }}
SortPanel, Canvaspublic class SortPanel extends Panel { SortThread thread ; GraphicCanvas can; int[] a; . . . . public void start() { a = new int[100]; for(int i=0; i < a.length; i++) a[i] = (int)(200*Math.random()); thread = new SortThread(can, algo, a); thread.start(); }}
public class GraphicCanvas extends Canvas { . . . . public void swap(int i, int j) { lo = i; hi = j; } public void update(Graphics g) { g.clearRect(0,0,200,200); Color cl = g.getColor(); for(int i=0; i < sp.a.length; i++) { g.setColor((i == lo || i == hi)?
• runnable – može bežať, keď mu bude pridelený CPU
• dead – keď skončí metóda run(), resp. po stop()
• blocked – niečo mu bráni, aby bežal– sleep(miliseconds) – počká daný čas, ak nie je interrupted...– wait(), resp. wait (miliseconds) čaká na správu notify() resp. notifyAll() ,– čaká na I/O,– pokúša sa zavolať synchronized na metódu. resp. objekt ktorý je už v tom...
sleep vs. wait keď proces volá wait(), výpočet je pozastavený, ale iné synchronizované metódy môžu
public class Reminder { Timer timer; public Reminder(int seconds) { timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000);
} class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); timer.cancel(); //Terminate the thread } } public static void main(String args[]) { new Reminder(5); System.out.println("Task scheduled."); }}
•schedule(TimerTask task, long delay, long period) •schedule(TimerTask task, Date time, long period) •scheduleAtFixedRate(TimerTask task, long delay, long period) •scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
//Get the Date corresponding to 11:01:00 pm today.Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, 23);calendar.set(Calendar.MINUTE, 1);calendar.set(Calendar.SECOND, 0);Date time = calendar.getTime();
timer = new Timer();timer.schedule(new RemindTask(), time);