?? threading-ways.txt
字號(hào):
/* copy from P174~175 on Chinese version of Just Java 2
irrelevant threading
to speak out the favorite drink */
public class Drinks{
public static void main(String[] a){
Coffee t1 new Coffee();
T1.start();
New Tea().start(); // an anonymous thread
}
}
class Coffee extends Thread{
public void run(){
while(true){
System.out.prinln(“I prefer coffee. ”);
yield();
}
}
}
class Tea extends Thread{
public void run(){
while(true){
System.out.prinln(“I prefer tea. ”);
yield();
}
}
}
/* copy from P176~177 on Chinese version of Just Java 2
relevant, non-synchronized threading
to find out a prime */
public class testPrime{
public static void main(String s[]){
long possPrime = Long.parseLong(s[0]);
int centuries = (int)(possPrime/100)+1;
for(int i=0; i<centuries; i++){
new testRange(i*100, possPrime).start();
}
}
}
class testRange extends Thread{
static long possPrime;
long from, to;
testRange(int argFrom, long argpossPrime){
possPrime = argpossPrime;
if(argFrom=0) from=2;
else from=argFrom;
to = argFrom + 99;
}
public void run(){
for(long i=from; i<=to && i<possPrime; i++){
if(possPrime%i == 0){
System.out.println(“factor ”+i+“ found by thread “+getName());
break;
}
yield();
}
}
}
/* copy from P180~181 on Chinese version of Just Java 2
exclusive threading
to check the air pressure to keep it in range */
public class p{
static int pressureGauge=0;
static final int safetyLimit=20;
public static void main(String[] args){
pressure []p1 = new pressure[10];
for(int i=0; i<10; i++){
p1[i] = new pressure();
p1[i].start();
}
try{
for(int i=0; i<10; i++){
p1[i].join();
} catch(Exception e){}
System.out.print(“gauge reads”+pressureGauge);
System.out.println(“; safe limit is 20. ”);
}
}
class pressure extends Thread{
static synchronized void RaisePressure(){
if(p.pressureGauge < p.safetyLimit-15){
try{sleep(100); } catch(Exception e){} // delay
p.pressureGauge += 15;
}
else ; // pressure too high
}
public void run(){
RaisePressure();
}
}
/* copy from P187~189 on Chinese version of Just Java 2
alternate exclusive threading
to check the air pressure to keep it in range */
public class plum{
public static void main(String args[]){
Producer p = new Producer();
Consumer c = new Consumer(p);
p.start();
c.start();
}
}
class Producer extends Thread{
private String [] buffer = new String[8];
private int pi = 0; // produce index
private int gi = 0; // get index
public void run(){
for(;;) produce();
}
private final long start = System.currentTimeMillis();
private final String banana(){
return “” + (int) (System.currentTimeMillis() - start);
}
synchronized void produce(){
while (pi-gi+1 > buffer.length){
try{wait(); } catch(Exception e){}
} // while there is no room in the buffer
buffer[pi&0x7] = banana();
System.out.println(“produced[” +(pi&0x7) +”]” +buffer[pi&0x7]);
pi++;
notifyAll();
}
synchronized String consume(){
while (pi==gi){
try{wait(); } catch(Exception e){}
} // while there is nothing left to take from the buffer
notifyAll();
return buffer[ gi+&0x7 ];
}
}
class Consumer extends thread{
Producer whoIamTalkingTo;
Consumer (Producer who){
whoIamTalkingTo = who;
} // Java idiom for a constructor
public void run(){
java.util.Random r = new java.util.Random();
for(;;){
String result = whoIamTalkingTo.consume();
System.out.println(“consumed: ” + result);
// next line is just to make it run a bit slower
int randomtime = Math.abs(r.nextint() % 250);
try{sleep(randomtime); } catch(Exception e){}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -