鍐欎袱涓嚎绋嬶紝涓€涓嚎绋嬫墦鍗?-52锛屽彟涓€涓嚎绋嬫墦鍗癆-Z锛屾墦鍗伴『搴忎负12A34B56C......5152Z

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍐欎袱涓嚎绋嬶紝涓€涓嚎绋嬫墦鍗?-52锛屽彟涓€涓嚎绋嬫墦鍗癆-Z锛屾墦鍗伴『搴忎负12A34B56C......5152Z相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ide' title='ide'>ide   wait   ++   except   let   sync   super   adp   cat   

棰樼洰锛?/h2>

鍐欎袱涓嚎绋嬶紝涓€涓嚎绋嬫墦鍗?-52锛屽彟涓€涓嚎绋嬫墦鍗癆-Z锛屾墦鍗伴『搴忎负12A34B56C......5152Z銆傝姹傜敤绾跨▼闂寸殑閫氫俊銆?/p>

/**
* 鍐欎袱涓嚎绋嬶紝绗竴涓嚎绋嬫墦鍗?-52锛岀浜屼釜绾跨▼鎵撳嵃A-Z锛屾墦鍗扮粨鏋滀负12A34B...5152Z
*/
public class ThreadPrinter {
// true鎵撳嵃鏁板瓧锛宖alse鎵撳嵃瀛楁瘝
private boolean flag = true;

// 鎵撳嵃瀛楁瘝
public synchronized void printNumber(String s) {
try {
if (!flag) {
super.wait();
}
System.out.print(s);
flag = false;
super.notifyAll();
} catch (InterruptedException ex) {
}
}

// 鎵撳嵃鏁板瓧
public synchronized void printLetter(String s) {
try {
if (flag) {
super.wait();
}
System.out.print(s);
flag = true;
super.notifyAll();
} catch (InterruptedException ex) {
}
}

/**
*绾跨▼2:鎵撳嵃A-Z
*/
public static class LetterPrintThread extends Thread {
private ThreadPrinter printer;

private LetterPrintThread(String name, ThreadPrinter printer) {
super(name);
this.printer = printer;
}

@Override
public void run() {
for (int i = 1; i <= 26; i++) {
int temp = i + 64;
char c = (char) temp;
printer.printLetter(c + " ");
}
}

}

/**绾跨▼1:鎵撳嵃1-52*/
public static class NumberPrintThread extends Thread {
private ThreadPrinter printer;

private NumberPrintThread(String name, ThreadPrinter printer) {
super(name);
this.printer = printer;
}
@Override
public void run() {
for (int i = 1; i <= 26; i++) {
String s = (2 * i - 1) + " " + 2 * i + " ";
printer.printNumber(s);
}

}
}

public static void main(String[] args) {
ThreadPrinter printer = new ThreadPrinter();
new NumberPrintThread("鎵撳嵃鏁板瓧绾跨▼", printer).start();
new LetterPrintThread("鎵撳嵃瀛楁瘝绾跨▼", printer).start();
}
}

以上是关于鍐欎袱涓嚎绋嬶紝涓€涓嚎绋嬫墦鍗?-52锛屽彟涓€涓嚎绋嬫墦鍗癆-Z锛屾墦鍗伴『搴忎负12A34B56C......5152Z的主要内容,如果未能解决你的问题,请参考以下文章

鎬庢牱鍒ゆ柇鐢ㄦ埛鏄惁鍦ㄧ嚎

NO.29 鏈夊涔冨ぇ锛氱嚎绋嬫睜

璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>