public class Test {
private static int k=0//售出票数
private static int count=1000//总票数
private static synchronized boolean 纤庆sell(int n){
if(k<count){
k++
System.out.println("第"+n+"售票点售出第"+k+"张票")
return true
}
return false
}
static class SellPot 毁郑握extends Thread{//售票点
int n//售丛汪票点编号
public SellPot(int n){
this.n=n
}
public void run(){
while(sell(n)){
try {
Thread.sleep(100)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
}
public static void main(String[] args) {
for(int i=1i<=10i++){
new SellPot(i).start()
}
}
}
//银行汇款
public class Test2 {
private static int k=0//总汇款
private static synchronized void send(int m,int i,int n){
k+=m
System.out.println("第"+n+"个顾客进行第"+(i+1)+"次汇款,银行接收总汇款"+k)
}
static class Costum extends Thread{//售票点
int n//顾客编号
public Costum(int n){
this.n=n
}
public void run(){
for(int i=0i<3i++){
send(100,i,n)//汇款100
try {
Thread.sleep(100)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
}
public static void main(String[] args) {
for(int i=1i<=3i++){
new Costum(i).start()
}
}
}
public class ShowDemo{public static void main(String[] rags)throws Exception{
MyThread mt= new MyThread()
Thread th1 = 槐唤new Thread(mt,"售票一")
Thread th2 = new Thread(mt,"段枝售票二")
Thread th3 = new Thread(mt,"售票三")
Thread th4 = 握明敏new Thread(mt,"售票四")
th1.start()th2.start()th3.start()th4.start()
}
}
class MyThread implements Runnable{
int ticket=1
public void run(){
while(ticket<=100){
if("售票一".equals(Thread.currentThread().getName()) && ticket%2!=0){
System.out.println("售票一售出:"+ticket++)
}
if("售票二".equals(Thread.currentThread().getName()) && ticket%2!=0){
System.out.println("售票二售出:"+ticket++)
}
if("售票三".equals(Thread.currentThread().getName()) && ticket%2==0){
System.out.println("售票三售出:"+ticket++)
}
if("售票四".equals(Thread.currentThread().getName()) && ticket%4==0){
System.out.println("售票四售出:"+ticket++)
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)