线程同步问题

线程同步问题之一

创建多线程实现3个窗口卖票,一共30张票,每个窗口各卖10张,一次卖一张(线程同步)

public class TicketDemo3 {
	public static void main(String[] args) {
		Station1 sta1 = new Station1();
		Thread t1=new Thread(sta1,"窗口一");
		Thread t2=new Thread(sta1,"窗口二");
		Thread t3=new Thread(sta1,"窗口三");
		t1.start();
		t2.start();
		t3.start();
	}
	
}
class Station1 extends Thread{
	static int tick = 20 ;
	@Override
	public  void run() {
		while (true) {
			synchronized (this) {
				notify();
				if (tick>0) {
					System.out.println(Thread.currentThread().getName()+"卖出第"+tick+"张票") ;
					tick--;
				}else {
					System.out.println("票卖完了");
					break;
				}
				try {
					wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
	}
	
}

线程同步问题之二

编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束
同步块方法

public class ThreadDemo {
	public static void main(String[] args) {
		Object obj = new Object();
		ThreadNumber tn = new ThreadNumber(obj);
		ThreadChar tc = new ThreadChar(obj);
		tn.start();
		tc.start();
	}
}
class ThreadNumber extends Thread {

private Object obj ;

	public ThreadNumber(Object obj) {
	super();
	this.obj = obj;
}
	@Override
	public void run() {
		synchronized (obj) {
			for (int i = 1; i <=52; i++) {
				System.out.print(i+" ");
				if (i%2==0) {
					obj.notifyAll();
					try {
						obj.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	
}
class ThreadChar extends Thread{
	private Object obj ;
	public ThreadChar(Object obj) {
		super();
		this.obj = obj;
	}
	@Override
	public void run() {
		synchronized (obj) {
			for (char c = 'A'; c<='Z'; c++) {
				System.err.print(c);
				try {
					Thread.sleep(200);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				obj.notifyAll();
				try {
					if (c!='Z') {
						obj.wait();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

同步方法

/**
 *一、同步方法
 * */
public class TestPrinter {
	public static void main(String[] args) {
		Printer p = new Printer();
		NumberPrinter np = new NumberPrinter(p);
		CharPrinter cp = new CharPrinter(p);
		np.start();
		cp.start();
	}
}
class Printer{
	private int index =1 ;
	public synchronized void print(int i ) {
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//判断index是否能被3整除
		while (index%3==0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.print(i);
		index++;
		this.notifyAll();//唤醒字母线程
	}
	public synchronized void print(char c) {
		try {
			Thread.sleep(200);//这里休眠无所谓
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		while (index%3!=0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.err.print(c);
		index++;
		this.notifyAll();//唤醒数字线程程
	}
}
//打印数字
class NumberPrinter extends Thread{
	private Printer p ;
	
	public NumberPrinter(Printer p) {
		super();
		this.p = p;
	}

	@Override
	public void run() {
		for (int i = 1; i <=52; i++) {
			p.print(i);
		}
	}
}
//打印字母
class CharPrinter extends Thread{
	private Printer p ;
	
	public CharPrinter(Printer p) {
		super();
		this.p = p;
	}
	@Override
	public void run() {
		for (char c= 'A';c<='Z';c++) {
			p.print(c);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值