JAVA and C++°æÐźÅÁ¿£¨PV²Ù×÷£©
import java.util.concurrent.Semaphore;
/**
* ³éÏóÈÎÎñ£¬¾ßÌåµÄÖ´ÐÐÈÎÎñ£¬¹éʵÏÖÀฺÔð
*
* @author Administrator
*
*/
public abstract class Task {
public abstract void run();
private Semaphore s;
private boolean hasExisted = false;
public void P(final Semaphore s) throws InterruptedException {
if (s == null) { // ÉêÇë¿ÕµÄÐźÅÁ¿
throw new InterruptedException("²»ÄÜΪ¿Õ");
}
if (hasExisted) {// ÒѾÉêÇëÁËÒ»¸ö×ÊÔ´£¬»¹Ã»ÓÐÊÍ·Å
throw new InterruptedException("ÒѾռÓÃÒ»¸ö×ÊÔ´");
}
s.acquire();// ×èÈû
this.s = s;
hasExisted = true;
}
public boolean V() {
if (!hasExisted) {
return false;// ûմÓÐ×ÊÔ´¾Í²»ÄÜ˵ÊÍ·ÅÁË
}
s.release();//ÊÍ·Å×ÊÔ´
hasExisted = false;
s = null;
return true;
}
}
import java.util.concurrent.Semaphore;
/**
* ÓÃÐźÅÁ¿ÊµÏÖ»¥³â
*
* @author Administrator
*
*/
public class Mutex extends Semaphore {
private static final long serialVersionUID = 1L;
public Mutex() {
super(1);//»¥³âÊÇ1
}
} import java.util.concurrent.Semaphore;
public class Test {
public static void main(String[] args) {
// final Semaphore s = new Semaphore(2);//ÐźÅÁ¿£¬Ö´Ðнá¹ûrun the task1 run the
// task2 run the task3
final Semaphore s = new Mutex();// »¥³â run the task1 run the task2 run
// the task3
new Thread() {
public void run() {
Task1 task2 = new Task1();
try {
task2.P(s);// ÉêÇë×ÊÔ´
task2.run();// Ö´ÐÐÈÎÎñ
task2.V();// ÊÍ·Å×ÊÔ´
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
Task2 task2 = new Task2();
try {
task2.P(s);
task2.run();
task2.V();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
Task3 task2 = new Task3();
try {
task2.P(s);
task2.run();
task2.V();
} catch (InterruptedException e)
Ïà¹ØÎĵµ£º
1 ×÷ÓÃÓò
javaÖÐ×÷ÓÃÓòµÄ·¶Î§ÓÉ»¨À¨ºÅ±êʾ¡£
2¹ØÓÚstatic
À¬»øÊÕ¼¯Æ÷Ö»ÖªµÀËѼ¯ÄÇЩÓÐnewÔËËã·û·ÖÅäµÄÄڴ棬²»ÖªµÀÈçºÎÊÍ·ÅÌØÊâÄÚ´æ¡£
Èç¹ûÀ¬»øÊÕ¼¯Æ÷×¼±¸»ØÊÕ¶ÔÏóµÄÄڴ棬ËüÊ×Ïȵ÷ÓÃfinalize()£¬¶øÇÒÖ»ÓÐÔÚÏÂÒ»´ÎÀ¬»øËѼ¯¹ý³ÌÖУ¬²Å»áÕæÕý»ØÊÕ¶ÔÏóµÄÄÚ´æ¡£ËùÒÔ˵Èç¹ûʹÓÃ
final ......
http://iccyx.cn/2007/04/21/java-console-text-area-input-stream/
ÍêÕûÇë¿´ÉÏURL
2001 Äê 10 ÔÂ 16 ÈÕ
ÔÚJava¿ª·¢ÖУ¬¿ØÖÆÌ¨Êä³öÈÔÊÇÒ»¸öÖØÒªµÄ¹¤¾ß£¬µ«Ä¬ÈϵĿØÖÆÌ¨Êä³öÓÐן÷ÖÖ¸÷ÑùµÄ¾ÖÏÞ¡£±¾ÎĽéÉÜÈçºÎÓÃJava¹ÜµÀÁ÷½ØÈ¡¿ØÖÆÌ¨Êä³ö£¬·ÖÎö¹ÜµÀÁ÷Ó¦ÓÃÖÐÓ¦¸Ã×¢ÒâµÄÎÊÌ⣬ÌṩÁ˽ØÈ¡Java³ÌÐòºÍ·ÇJava³ÌÐò¿ØÖÆÌ¨Êä³öµÄÊ ......
JavaÓëÆäËûÓïÑԵĻ¥²Ù×÷ÐÔ ÊÕ²Ø
Ŀǰ£¬ÎÒÃÇÏîÄ¿ÖÐÐèÒªÓõ½°ÑJava³ÌÐòǶÈëµ½.Net³ÌÐòÖеļ¼Êõ£¬ÎÒÃÇ×îºóËù²ÉÓõļ¼ÊõÊÇCodemesh¹«Ë¾µÄJuggerNET¡£ÕâÖÖÀàËÆµÄ¼¼ÊõºÜ¶à£¬±È½ÏÖªÃûµÄÖ÷ÒªÓÐintrinsyc¹«Ë¾µÄJIntegraÒÔ¼°IBMµÄJava COM bridge¡£
ÏîÄ¿½øÐе½ÏÖÔÚ£¬·¢ÏÖÁ˺ܶàÎÊÌ⣬Ö÷ÒªÓÐÈçϼ¸´óÀàÎÊÌ⣺
1¡¢µ¯³ö´°¿ÚÎÊÌâ
¶ÔÓ ......