创建两个线程,一个线程打印1---25
一个线程打印A-Z
输出效果是12A34B56C......5152Z
第一版不符合我们的要求(因为无法确定哪个线程先执行):
public class MyTest {
public static void main(String[] args) {
// 创建两个线程,一个线程打印 1---52
//一个线程打印 A-Z
//输出效果是 12A34B56C.......5152Z
//第一版不是很符合我们的要求
MyObject obj = new MyObject();
NumberRunnable numberRunnable = new NumberRunnable(obj);
CharRunnable charRunnable = new CharRunnable(obj);
Thread th1 = new Thread(numberRunnable);
Thread th2 = new Thread(charRunnable);
th1.start();
th2.start();
}
}
class MyObject{
}
class NumberRunnable implements Runnable{
private MyObject obj;
public NumberRunnable(MyObject obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i = 1; i <= 52; i++) {
synchronized (obj){
System.out.println(i); //1 2
if(i%2==0){
obj.notify(); //唤醒等待的线程
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
class CharRunnable implements Runnable{
private MyObject obj;
public CharRunnable(MyObject obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i ='A'; i <='Z'; i++) {
synchronized (obj){
System.out.println((char) i);
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
定义一个类型作为标记:
public class MyTest {
public static void main(String[] args) {
// 创建两个线程,一个线程打印 1---52
//一个线程打印 A-Z
//输出效果是 12A34B56C.......5152Z
MyObject obj = new MyObject();
NumberRunnable numberRunnable = new NumberRunnable(obj);
CharRunnable charRunnable = new CharRunnable(obj);
Thread th1 = new Thread(numberRunnable);
Thread th2 = new Thread(charRunnable);
th2.start();
th1.start();
}
}
class MyObject{
// boolean flag;
public int flag=1; //int 类型作为标记,可以多标记一下情况
}
class NumberRunnable implements Runnable{
private MyObject obj;
public NumberRunnable(MyObject obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i = 1; i <= 52; i++) {
synchronized (obj){
if(obj.flag!=1){ //1!=1 false
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//开始打印
System.out.println(i);
if(i%2==0){
obj.flag=2;
obj.notify(); //唤醒等待的线程
}
}
}
}
}
class CharRunnable implements Runnable{
private MyObject obj;
public CharRunnable(MyObject obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i ='A'; i <='Z'; i++) {
synchronized (obj){
if(obj.flag!=2){ //1!=2 true
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println((char) i);
//修改标记
obj.flag=1;
//唤醒等待的线程
obj.notify();
}
}
}
}
2.
两个线程,一个线程打印100个A,一个线程打印100个B
效果就是 AB AB AB AB......
public class MyTest {
public static void main(String[] args) {
//两个线程 一个线程打印100个A 1个线程打印 100个B
//效果就是AB AB AB AB...
MyObject obj = new MyObject();
AThread th1 = new AThread(obj);
BThread th2 = new BThread(obj);
th2.start();
th1.start();
}
}
class MyObject {
// boolean flag;
public int flag = 1; //int 类型作为标记,可以多标记一下情况
}
class AThread extends Thread {
private MyObject obj;
public AThread(MyObject obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
synchronized (obj) {
if (obj.flag != 1) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A");
obj.flag = 2;
obj.notify();
}
}
}
}
class BThread extends Thread {
private MyObject obj;
public BThread(MyObject obj) {
this.obj = obj;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
synchronized (obj) {
if (obj.flag != 2) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B");
obj.flag = 1;
obj.notify();
}
}
}
}



