重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
很简单, 出票里加锁就行了完整代码:
创新互联主要从事网站设计、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务顺庆,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
public class Test {
public static void main(String[] args) {
for(int i=0; i3; i++){
new Thread("线程 " + i){
public void run() {
while(true){
int p = getNumber();
if(p 0 ){
System.out.println(getName() + " 票号: " + p);
}else{
System.out.println("没票了");
break;
}
}
};
}.start();
}
}
public static int num = 100; //总票数
/**
* synchronized 同步锁
* @return
*/
public static synchronized int getNumber(){
if(num 0){
return num --; //如果大于0, 则返回当前票并减少一张
}
return 0;
}
}
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++);
}
}
}
}
你的这句话放在run方法里面了:Object b=new Object();
是局部变量,一直在变化,造成锁一直在变化,所以出现了问题。
public class Yugi implements Runnable
{
@Override
public void run()
{
String name = Thread.currentThread().getName();
while(name.startsWith("窗口"))
{
if(tickets.length == 0)
{
stop();
break;
}
int num = (int) (Math.random() * tickets.length) + 1;
tickets = new int[tickets.length - num];
System.out.println(name + " 售出了 " + num + " 张票");
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{}
}
}
public synchronized void start()
{
for(int i = 0; i ts.length; i++)
{
Thread thread = ts[i];
if(thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setName("窗口" + (i + 1));
thread.start();
}
}
}
public synchronized void stop()
{
for(int i = 0; i ts.length; i++)
{
Thread thread = ts[i];
if(thread != null)
{
thread.interrupt();
}
thread = null;
}
notifyAll();
}
static int[] tickets = new int[100];
private static int WIN = 5;
Thread[] ts = new Thread[WIN];
public static void main(String[] args)
{
new Yugi().start();
}
}
首先,定义的锁(lock)不对,必须是同一个锁,像你这样用this,new多少个MyThread就有多少个锁,违反了线程的同步机制;
其次,你如果想要呈现多线程的竞争,不可以在run方法里让一个线程一直运行而不释放锁,应该使用wait()/notify();
以下我稍微修改下,会形成两个线程交替执行的情形:
public class ThreadTest {
public static Object obj = new Object();
public static int number = 10;// 用static共享票源,不知道可以不,按理说是可以的
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread my = new MyThread();
MyThread my2 = new MyThread();
Thread t1 = new Thread(my2, "2号窗口");
Thread t2 = new Thread(my, "1号窗口");
t1.start();
t2.start();
}
static class MyThread implements Runnable {
public void run() {
while (true)
synchronized (obj) {
if (number 0) {
System.out.println(Thread.currentThread().getName()
+ "正在卖票," + "还剩" + number + "张票");
number--;
obj.notifyAll();
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
break;
}
}
}
}