三线程顺序打印N次ABC

记得前一阵子JE上讨论线程顺序打印的面试题,现在有空也练练。
说是,三个线程,其一只打印A,其一只打印B,其一只打印C,要求开启三线程顺序打印多次ABC。
对wait和notify(All)刚刚理解,不能错过这个练习的机会,然后再写个生产者-消费者的程序多练练。写的不好的、需要改进的地方还请JE上的朋友给建议^..^。

public class ThreadABC implements Runnable {
public static final int RUN_TOTAL_COUNT=30; //打印ABC的次数
public static volatile boolean arun=true; //开始打印A
public static volatile boolean brun=false;
public static volatile boolean crun=false;
public static final byte[] LOCK=new byte[0]; //互斥器
public void run() {
new Thread(new PrintB()).start();
new Thread(new PrintC()).start();
new Thread(new PrintA()).start();
}
private static class PrintA implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(arun){
LOCK.notifyAll();
System.out.print("A");
runCount++;
arun=false;
brun=true;
crun=false;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintA InterruptedException occured...");
}
}
} //end syn
}
}
private static class PrintB implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(brun){
LOCK.notifyAll();
System.out.print("B");
runCount++;
arun=false;
brun=false;
crun=true;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintB InterruptedException occured...");
}
}
} //end syn
}
}
private static class PrintC implements Runnable{
private int runCount=0;
public void run(){
synchronized(LOCK){
while(runCount<RUN_TOTAL_COUNT)
if(crun){
LOCK.notifyAll();
System.out.print("C-");
runCount++;
arun=true;
brun=false;
crun=false;
}else{
try{
LOCK.wait();
}catch(InterruptedException ie){
System.out.println("PrintC InterruptedException occured...");
}
}
} //end syn
}
}

//test in main
public static void main(String[] args){
new Thread(new ThreadABC()).start();
}
}

-----------------顺序打印A-Z-----------------------------------

public class Print implements Runnable {
public static final int COUNT=26; //字母的个数
public static final int TIMES=10; //循环打印的次数
public static final byte[] LOCK=new byte[0]; //互斥器
private static volatile int run=0; //每打印一个字母,值会+1
private List<Printor> printors=new ArrayList<Printor>(); //收集所有线程对象

@Override
public void run() {
for(int i=0;i<COUNT;i++){ //初始化26个线程对象
printors.add(new Printor((char)('A'+i), i));
}
for(int i=printors.size()-1;i>=0;i--){ //逆序:依次运行
new Thread(printors.get(i)).start();
}
}
//test in main
public static void main(String[] args){
new Thread(new Print()).start();
}

private static class Printor implements Runnable{
private char name;
private int id;
private int times=0;
public Printor(char name,int id){
this.name=name;
this.id=id;
}
@Override
public void run() {
synchronized(LOCK){ //把门锁上,别人别想进来
while(this.times<TIMES)
if(this.id==run%COUNT){
System.out.print(this.name);
if(this.id==COUNT-1) System.out.println(); //换行
run++;
this.times++;
LOCK.notifyAll(); //吼一嗓子:其他的兄弟,你们准备好了吗
}else {
try {
LOCK.wait(); //我K,白来了,我得出去等(释放锁)...
} catch (InterruptedException e) {
System.out.println(this.name+"=>exception occurs...");
}
}
} //结束:释放锁
}
}
}
#include <STC8H.H> //sbit CS = P2^0; // 片选信号 //sbit SID = P2^1; // 数据线 //sbit CLK = P2^2; // 时钟线 #define CS P20 #define SID P21 #define CLK P22 // 微秒级延时(12MHz晶振) void Delay_us(unsigned int t) { while(t--); } // 串行发送一个字节(高位在前) void SPI_WriteByte(unsigned char byte) { unsigned char i; CS = 1; // 使能片选 for(i=0; i<8; i++) { CLK = 0; // 时钟拉低 SID = (byte & 0x80); // 取最高位 byte <<= 1; // 左移准备下一位 CLK = 1; // 时钟上升沿发送数据 Delay_us(5); // 保持稳定时间 } CS = 0; // 关闭片选 } // 写命令(串行模式固定格式) void LCD_WriteCommand(unsigned char cmd) { SPI_WriteByte(0xF8); // 命令头:11111[RW=0][RS=0]0 SPI_WriteByte(cmd & 0xF0); // 高4位 SPI_WriteByte(cmd << 4); // 低4位(左移对齐) } // 写数据 void LCD_WriteData(unsigned char dat) { SPI_WriteByte(0xFA); // 数据头:11111[RW=0][RS=1]0 SPI_WriteByte(dat & 0xF0); // 高4位 SPI_WriteByte(dat << 4); // 低4位 } // LCD初始化(串行模式) void LCD_Init() { Delay_us(50000); // 上电延时 >40ms LCD_WriteCommand(0x30); // 基本指令集 LCD_WriteCommand(0x0C); // 显示开,无光标 LCD_WriteCommand(0x01); // 清屏 LCD_WriteCommand(0x06); // 地址递增模式 } // 显示字符串(自动调用内置字库) void LCD_Print(unsigned char x, unsigned char y, char *str) { unsigned char addr; // 设置显示位置(最大4行,每行8汉字) if(y == 0) addr = 0x80 + x; else if(y == 1) addr = 0x90 + x; else if(y == 2) addr = 0x88 + x; else addr = 0x98 + x; // 第3行 LCD_WriteCommand(addr); // 设置地址 while(*str) { LCD_WriteData(*str++); // 写入GB2312编码 } } //但是显示中文有问题
最新发布
09-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值