疯狂习题:写2个线程,其中一个线程打印1-52,另一个线程打印A-Z,打印的顺序应该是12A34B56C...5152Z。 10

packagehz89.me.test;publicclasstest01{publicstaticvoidmain(String[]args){Printprint=n... package hz89.me.test;

public class test01 {

public static void main(String[] args) {
Print print = new Print();
new printNum(print).start();
new printChar(print).start();

}

}

class Print {

private boolean flag = true;

public synchronized void printNum(int i) {
try {
if (flag) {
System.out.print(i);
if (i % 2 == 0) {
flag = false;
notifyAll();
}
} else {
wait();
}
} catch (Exception e) {
e.printStackTrace();
}

}

public synchronized void printChar(char chars) {
try {
if (!flag) {
System.out.print(chars);
flag = true;
notifyAll();
} else {
wait();
}

} catch (Exception e) {
e.printStackTrace();
}
}

}

class printNum extends Thread {

Print p = new Print();

public printNum(Print p) {
this.p = p;
}

@Override
public void run() {
for (int i = 1; i < 53; i++) {
p.printNum(i);
}
}
}

class printChar extends Thread {

Print p = new Print();

public printChar(Print p) {
this.p = p;
}

@Override
public void run() {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < str.length(); i++) {
p.printChar(str.charAt(i));
}
}
}

为啥打印出来是这个呢?
12A4C6E8G10I12K14M16O18Q20S22U24W26Y28。。。
求助!!!
展开
 我来答
kingkate2009
2017-02-28 · TA获得超过535个赞
知道小有建树答主
回答量:557
采纳率:50%
帮助的人:250万
展开全部
看我的

package test;

public class NumThread extends Thread {

private Printer printer;

public NumThread(Printer printer) {
this.printer = printer;
}

@Override
public void run() {
for(int i=1;i<=52;i++){
synchronized (printer) {
if(i % 2 ==0){
printer.print(i);
printer.notify();
}else {
printer.print(i);
}

}
if(i % 2 ==0){
synchronized (printer) {
try {
printer.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

}
public Printer getPrinter() {
return printer;
}
public void setPrinter(Printer printer) {
this.printer = printer;
}

}

package test;

public class CharThread extends Thread {

private Printer printer;

public CharThread(Printer printer) {
this.printer = printer;
}
@Override
public void run() {
for(int i=0;i<26;i++){
synchronized (printer) {
printer.print((char)('A'+i));
printer.notify();

}
if(i== 25){
return;
}
synchronized (printer) {
try {
printer.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Printer getPrinter() {
return printer;
}
public void setPrinter(Printer printer) {
this.printer = printer;
}

}

package test;

public class Printer {

public void print(Object obj){
System.out.print(obj.toString());
}
}
package test;

public class TestPrint {

public static void main(String[] args) {
Printer printer = new Printer();
Thread numThread = new Thread(new NumThread(printer));
Thread charThread = new Thread(new CharThread(printer));
numThread.start();
charThread.start();
}
}
//下面说明
两个线程循环打印数据,属于线程间通信,因此如果要线程间通信,就需要两个线程执行时获取同一把锁(这里为Printer对象)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式