利用线程的通信机制,用两个线程打印以下的结果: 1 2 A 3 4 B 5 6 C 7 8 D ... 49 50 Y 51 52 Z 5
package com.myThread;
public class TestMain {
public static Object object = new Object();//共享资源
public static boolean flag = true;//判断是否线程要结束
public static void main(String[] args) {
Thread t1 = new Thread(new ThreadNumber(object));
Thread t2 = new Thread(new ThreadChar(object));
t1.start();
t2.start();
}
}
//打印数字线程
class ThreadNumber implements Runnable {
private Object object;
public ThreadNumber(Object object) {
this.object = object;
}
@Override
public void run() {
while(TestMain.flag){
synchronized (object) {//共享资源上锁
for (int i = 1; i < 110; i++) {
//如果别的线程结束了,自己也跟着结束
if (!TestMain.flag) {
break;
}
System.out.print(i);
if (i % 2 == 0) {
//唤醒其他线程打印
object.notifyAll();
try {
//本身进入等待
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//自己打印完了,就设置结束标志为false,通知其他线程也结束
TestMain.flag = false;
//唤醒其他线程,让其他线程有机会继续,而不会一直处于等待
object.notifyAll();
}
}
}
}
//打印字母线程
class ThreadChar implements Runnable {
private Object object;
public ThreadChar(Object object) {
this.object = object;
}
@Override
public void run() {
String s[] = "A,B,C".split(",");
while(TestMain.flag){
synchronized (object) {//共享资源上锁
for(String string : s){
//如果别的线程结束了,自己也跟着结束
if (!TestMain.flag) {
break;
}
System.out.print(string);
object.notifyAll();
try {
//本身进入等待
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//自己打印完了,就设置结束标志为false,通知其他线程也结束
TestMain.flag = false;
//唤醒其他线程,让其他线程有机会继续,而不会一直处于等待
object.notifyAll();
}
}
}
}
亲测有效
public class Communication {
public static void main(String[] args) {
Object obj = new Object();
Thread t1 = new NumberThread2(obj);
Thread t2 = new StringThread2(obj);
t1.start();
t2.start();
}
}
class NumberThread2 extends Thread {
private Object obj;
public NumberThread2(Object obj) {
super();
this.obj = obj;
}
public void run() {
synchronized (obj) {
for (int i = 1; i <= 26; i++) {
System.out.print(i*2-1 + "\t");
System.out.print(i*2 + "\t");
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class StringThread2 extends Thread {
private Object obj;
public StringThread2(Object obj) {
super();
this.obj = obj;
}
public void run() {
synchronized(obj){
for (char ch = 'A'; ch <= 'Z'; ch++) {
System.out.print(ch + "\t");
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2013-08-07
public class MyTestThread1 {
public static void main(String[] args) {
Object obj = new Object();
Thread1 t1 = new Thread1(obj);
t1.start();
Thread2 t2 = new Thread2(obj);
Thread thread = new Thread(t2);
thread.start();
}
}
class Thread1 extends Thread{
private Object obj;
public Thread1(Object obj) {
this.obj = obj;
}
@Override
public void run() {
synchronized (obj) {
for(int i = 1;i <= 26;i++){
System.out.println(2*i - 1);
System.out.println(2*i);
}
}
}
}
class Thread2 implements Runnable{
private Object obj;
public Thread2(Object obj) {
this.obj = obj;
}
public void run() {
synchronized (obj) {
for(char c ='A';c <='Z';c++){
System.out.println(c);
}
}
}
}