写两个线程,一个线程打印1~52,另一个线程打印字母A-Z.打印顺序为12A34B56C……5152Z.用线程间的通信。
publicclassDemo{staticbooleanflag=true;publicstaticvoidmain(String[]args){Testt=newTe...
public class Demo {
static boolean flag = true;
public static void main(String[] args) {
Test t = new Test();
Test2 t2 = new Test2();
Thread thread = new Thread(t);
Thread thread2 = new Thread(t2);
thread.start();
thread2.start();
}
}
public void printNum() throws Exception {
synchronized (Test.class) {
for (int i = 1; i <= s; i++) {
if (Demo.flag == true) {
System.out.print(i);
count++;
if (count == 2) {
count = 0;
Demo.flag = false;
}
} else {
notifyAll();
wait();
}
}
}
}
@Override
public void run() {// flag=true运行thread1,false 运行2
// TODO 自动生成的方法存根
try {
printNum();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public class Test2 implements Runnable {
int s = 'Z';
public void printChar() {
synchronized (Test2.class) {
for (char i = 'A'; i <= s; i++)
{
if (Demo.flag == false) {
System.out.print(i);
Demo.flag = true;
} else {
notifyAll();
try {
wait();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
}
@Override
public void run() {
// TODO 自动生成的方法存根
printChar();
}
} 展开
static boolean flag = true;
public static void main(String[] args) {
Test t = new Test();
Test2 t2 = new Test2();
Thread thread = new Thread(t);
Thread thread2 = new Thread(t2);
thread.start();
thread2.start();
}
}
public void printNum() throws Exception {
synchronized (Test.class) {
for (int i = 1; i <= s; i++) {
if (Demo.flag == true) {
System.out.print(i);
count++;
if (count == 2) {
count = 0;
Demo.flag = false;
}
} else {
notifyAll();
wait();
}
}
}
}
@Override
public void run() {// flag=true运行thread1,false 运行2
// TODO 自动生成的方法存根
try {
printNum();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public class Test2 implements Runnable {
int s = 'Z';
public void printChar() {
synchronized (Test2.class) {
for (char i = 'A'; i <= s; i++)
{
if (Demo.flag == false) {
System.out.print(i);
Demo.flag = true;
} else {
notifyAll();
try {
wait();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
}
@Override
public void run() {
// TODO 自动生成的方法存根
printChar();
}
} 展开
展开全部
Object lock = new Object();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
int n = 'A';
while (n < 'Z' + 1) {
synchronized (lock) {
System.out.print((char) n++);
lock.notify();
try {
if (n <= 'Z')
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
int n = 1;
while (n < 53) {
synchronized (lock) {
System.out.print(n++);
System.out.print(n++);
lock.notify();
try {
if (n <= 52)
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
thread2.start();
thread1.start();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
int n = 'A';
while (n < 'Z' + 1) {
synchronized (lock) {
System.out.print((char) n++);
lock.notify();
try {
if (n <= 'Z')
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
int n = 1;
while (n < 53) {
synchronized (lock) {
System.out.print(n++);
System.out.print(n++);
lock.notify();
try {
if (n <= 52)
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
thread2.start();
thread1.start();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询