Java 多线程实例

1.多窗口同时买票:

package com.qst.dms.Server;

import java.awt.dnd.DnDConstants;
import java.util.concurrent.locks.ReentrantLock;

//处理并发
public class Servebuytickts {
	int num;
	private  final ReentrantLock  lock= new ReentrantLock();
	public Servebuytickts(int num)
	{
		this.num=num;
	}
//	1.可以生成一个同步方法
//	public synchronized void buytickts()
//	{
//		System.out.print(Thread.currentThread().getName());
//		if(num<=0)
//		{
//			System.out.println("票卖完了.");
//		}
//		else 
//		{
//			num--;
//			System.out.println("还剩下"+num+"张票");
//		}
//	}
//  2.可以加上同步锁
	public void buytickts()
	{
		lock.lock();
		System.out.print(Thread.currentThread().getName());
		if(num<=0)
		{
			System.out.println("票卖完了.");
		}
		else 
		{
			num--;
			System.out.println("还剩下"+num+"张票");
		}
		lock.unlock();
	}
	public static void main(String args[])
	{
		Servebuytickts sb = new Servebuytickts(3);
		Thread t[] = new Thread[5];
		for (int i=0;i<5;i++)
		{
			t[i] = new Thread(new customer(sb),Integer.toString(i+1));
		}
		for (int i=0;i<5;i++)
		{
			t[i].start();
		}
	}
}
//顾客抢票
class customer implements Runnable
{
	Servebuytickts sb;
	public customer(Servebuytickts sb) {
		// TODO Auto-generated constructor stub
		this.sb=sb;
	}
	public void run()
	{
		this.sb.buytickts();
	}
}

运行结果:

3还剩下2张票
2还剩下1张票
1还剩下0张票
4票卖完了.
5票卖完了.

2. 银行存钱与取钱业务

package com.qst.dms.Server;

import org.omg.CORBA.PUBLIC_MEMBER;

//银行账户类
public class BankAccount1{
	private String name;
	private int balance;
	public BankAccount1(String name,int balance)
	{
		this.name=name;
		this.balance=balance;
	}
	//设置取钱和存钱实现的共同方法
	public synchronized void service (int money)
	{
		System.out.print("process"+Thread.currentThread().getName()+": ");
		if(money<0&&-money>balance)
		{
			System.out.println("余额不足");
		}
		else 
		{
			try
			{
				balance+=money;
				System.out.println("余额为"+balance);
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
	public static void main(String args[])
	{
		BankAccount1 ba1 = new BankAccount1("xiaoming", 1000);
		service t[] = new service[5];
		for (int i=0;i<5;i++)
		{
			int money= i%2==0? (i+1)*1000:-(i+1)*1000;
			t[i] = new service(Integer.toString(i+1),ba1,money);
		}
		for (int i=0;i<5;i++)
		{
			t[i].start();
		}
	}
}
//创建进程
class service extends Thread
{
	BankAccount1 ba1;
	int money;
	public service(String name,BankAccount1 ba1,int money) {
		// TODO Auto-generated constructor stub
		super(name);
		this.ba1=ba1;
		this.money=money;
	} 
	public void run()
	{
		ba1.service(money);
	}
}

运行结果:

process2: 余额不足
process5: 余额为6000
process3: 余额为9000
process4: 余额为5000
process1: 余额为6000

3. 龟兔赛跑:200米 
要求:
    (1)兔子每 0.1 秒 5 米的速度,每跑20米休息1秒;
    (2)乌龟每 0.1 秒跑 2 米,不休息;
    (3)其中一个跑到终点后另一个不跑了!

只需要各个实体存一下对手的对象,然后改变对手实例化对象的isCanel,使其跳出进程函数即可。

Animal 抽象类:

package com.qst.dms.Server;

public abstract class Animal extends Thread{
	static int length=200;
	protected volatile boolean  isCancel=false;
	Animal animal;
	public void getAnimal(Animal animal)
	{
		this.animal=animal;
	}
	public Animal (String name)
	{
		super(name);
	}
	public abstract void running();
	public void Stop()
	{
		isCancel=true;
	}
	
}

Rabbit  实体类:

package com.qst.dms.Server;

public class Rabbit  extends Animal{
	public Rabbit (String name)
	{
		super(name);
	}
	public void running()
	{
		int tlen=0;
		while(tlen<length&&isCancel==false)
		{
			for (int i=1;i<=4;i++)
			{
				try{
					sleep(100);
				}catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				tlen+=5;
				if(isCancel==true) break;
				System.out.println("兔子跑了"+tlen+"的距离");
				if(tlen>=length) break;
			}
			try {
				sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if(isCancel==false) System.out.println("兔子获得了胜利");
		this.animal.Stop();
	}
	public void run()
	{
		running();
	}
}

Tortoise实体类:

package com.qst.dms.Server;

public class Tortoise extends Animal{
	public Tortoise(String name)
	{
		super(name);
	}
	public void running()
	{
		int tlen=0;
		while(tlen<length&&isCancel==false)
		{
			try{
				sleep(100);
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			tlen+=2;
			System.out.println("乌龟跑了"+tlen+"的距离");
		}
		if(isCancel==false) System.out.println("乌龟获得了胜利");
		this.animal.Stop();
	}
	public void run()
	{
		running();
	}
}

主运行窗口:

package com.qst.dms.Server;

public class MainClass {
	public  static void main(String args[])
	{
		Rabbit rabbit = new Rabbit("兔子");
		Tortoise tortoise =new Tortoise("乌龟");
		rabbit.getAnimal(tortoise);
		tortoise.getAnimal(rabbit);
		rabbit.start();
		tortoise.start();
	}
}

运行结果:

乌龟跑了142的距离
兔子跑了115的距离
乌龟跑了144的距离
兔子跑了120的距离
乌龟跑了146的距离
乌龟跑了148的距离
乌龟跑了150的距离
乌龟跑了152的距离
乌龟跑了154的距离
乌龟跑了156的距离
乌龟跑了158的距离
乌龟跑了160的距离
乌龟跑了162的距离
乌龟跑了164的距离
乌龟跑了166的距离
兔子跑了125的距离
乌龟跑了168的距离
兔子跑了130的距离
乌龟跑了170的距离
兔子跑了135的距离
乌龟跑了172的距离
兔子跑了140的距离
乌龟跑了174的距离
乌龟跑了176的距离
乌龟跑了178的距离
乌龟跑了180的距离
乌龟跑了182的距离
乌龟跑了184的距离
乌龟跑了186的距离
乌龟跑了188的距离
乌龟跑了190的距离
乌龟跑了192的距离
乌龟跑了194的距离
兔子跑了145的距离
乌龟跑了196的距离
兔子跑了150的距离
乌龟跑了198的距离
兔子跑了155的距离
乌龟跑了200的距离
乌龟获得了胜利

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值