* 写两个线程,一个线程打印1-52,另一个线程答应字母A-Z。
* 打印顺序为12A34B56C……5152Z。通过使用线程之间的通信协调关系。
- 注:分别给两个对象构造一个对象o,数字每打印两个或字母每打印一个就执行o.wait()。
- 在o.wait()之前不要忘了写o.notify()
代码:
方法一:直接写
package Homework;
public class Test2 {
public static void main(String[] args) {
O o=new O();
Threadnum threadnum=new Threadnum(o);
Threadabc threadabc=new Threadabc(o);
Thread thread=new Thread(threadnum);
Thread thread2=new Thread(threadabc);
thread.start();
thread2.start();
}
}
//资源类线程O
class O{
public synchronized void num(){
/*for(int i=1;i<=52;i++){
System.out.print(i);
if (i % 2 == 0) {
this.notify();
try {
this.wait();
} catch (InterruptedException e) {
// TODO 待完善
e.printStackTrace();
}
}*/
for(int i=0;i<26;i++){
this.notify();
for(int j=1+2*i;j<2*i+3;j++){