[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("No answer: interrupted"); } }}
学编程,从w3cschool开始,w3cschool推出的编程微课,采用游戏化的编程闯关模式,让你快速掌握编程这一门手艺。微课学习入口:W3Cschool编程微课
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
Java之父22年前写的一段代码
-
免费下载或者VIP会员资源能否直接商用?本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
-
提示下载完但解压或打开不了?最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或联络我们。
-
找不到素材资源介绍文章里的示例图片?对于会员专享、整站源码、程序插件、网站模板、网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
-
付款后无法显示下载地址或者无法查看内容?如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理
-
购买该资源后,可以退款吗?源码素材属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源
