App下載

Java之父22年前寫的一段代碼

猿友 2018-02-11 18:46:34 瀏覽數(shù) (15541)
反饋

[Listing One]

PingPong class PingPong extends Thread { String word; // what word to print int delay; // how long to pause

PingPong(String whatToSay, int delayTime) {
    word = whatToSay;
    delay = delayTime;
}

public void run() {
    try {
        for (;;) {
            System.out.print(word + " ");
            sleep(delay);   // wait until next time
        }
    } catch (InterruptedException e) {
        return;             // end this thread
    }
}
public static void main(String[] args) {
    new PingPong("ping",  33).start(); // 1/30 second
    new PingPong("PONG", 100).start(); // 1/10 second
}

}

[Listing Two]

Account class Account { private double balance;

public Account(double initialDeposit) {
    balance = initialDeposit;
}
public synchronized double getBalance() {
    return balance;
}
public synchronized void deposit(double amount) {
    balance += amount;
}

}

[Listing Three]

synchronized_abs /* make all elements in the array nonnegative / public static void abs(int[] values) { synchronized (values) { for (int i = 0; i < values.length; i++) { if (values[i] < 0) values[i] = -values[i]; } } }

[Listing Four]

class Queue { // The first and last elements in the queue Element head, tail;

public synchronized void append(Element p) {
    if (tail == null)
        head = p;
    else
        tail.next = p;
    p.next = null;
    tail = p;
    notify();  // Let waiters know something arrived
}

public synchronized Element get() {
    try {
        while(head == null)
            wait();    // Wait for an element
    } catch (InterruptedException e) {
        return null;
    }

    Element p = head;  // Remember first element
    head = head.next;  // Remove it from the queue
    if (head == null)  // Check for an empty queue
        tail = null;
    return p;
}

}

[Listing Five]

Thread spinner; // the thread doing the processing

public void userHitCancel() { spinner.suspend(); // whoa! if (askYesNo("Really Cancel?")) spinner.stop(); // stop it else spinner.resume(); // giddyap! }

[Listing Six]

class CalcThread extends Thread { private double Result;

public void run() {
    Result = calculate();
}

public double result() {
    return Result;
}

public double calculate() {
    // ...
}

}

class Join { public static void main(String[] args) { CalcThread calc = new CalcThread(); calc.start(); doSomethingElse(); try { calc.join(); System.out.println("result is "

            + calc.result());
    } catch (InterruptedException e) {
        System.out.println(&quot;No answer: interrupted&quot;);
    }
}

}

學(xué)編程,從w3cschool開始,w3cschool推出的編程微課,采用游戲化的編程闖關(guān)模式,讓你快速掌握編程這一門手藝。微課學(xué)習(xí)入口:W3Cschool編程微課

0 人點贊