java的多线程买票问题,结果出现负数情况
如果线程为前面两个会出现票号为“0”的情况,如果线程大于2个的话,就会出现负数,请教该如果解决问题呢?
[code="java"]
public class myFirst
{
public static void main(String[] args)
{
Q q = new Q();
new Thread(new SaleS(q)).start();//取单号票
new Thread(new SaleD(q)).start();//取双号票
new Thread(new SaleS(q)).start();//取单号票
new Thread(new SaleD(q)).start();//取双号票
new Thread(new SaleS(q)).start();//取单号票
}
}
class Q
{
private int tickets = 10;
public synchronized int get()
{
return tickets;
}
public synchronized void sale()
{
if(tickets>0)
{
System.out.println(Thread.currentThread().getName()+
" is salling ticket "+ tickets--);
try
{
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
public synchronized void sale(boolean b)
{
if(tickets>0)
{
if((b && tickets % 2 ==1) || (!b && tickets % 2 ==0))
{
try {
wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/*try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}*/
System.out.println(Thread.currentThread().getName()+
" is salling ticket "+ tickets--);
notify();
}
}
public synchronized void saleSingle()
{
if(tickets>0)
{
if(tickets % 2 ==0)
{
System.out.println(Thread.currentThread().getName()+
" is waitting ");
try {
wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+
" is salling ticket "+ tickets--);
notify();
}
}
public synchronized void saleDouble()
{
if(tickets>0)
{
if(tickets % 2 ==1)
{
System.out.println(Thread.currentThread().getName()+
" is waitting ");
try {
wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+
" is salling ticket "+ tickets--);
notify();
}
}
}
class SaleS implements Runnable
{
Q q = null;
public SaleS(Q q)
{this.q = q;}
public void run()
{
while(q.get()>0)
{
q.saleSingle();
}
}
}
class SaleD implements Runnable
{
Q q = null;
public SaleD(Q q)
{this.q = q;}
public void run()
{
while(q.get()>0)
{
q.saleDouble();
}
}
}
[/code]
当然会出现负了。。你ticket是个成员变量,你把每个方法synchronized,但是线程中调运个了2个方法,所以出现并发问题,应该用synchronized(this){}把两个方法都括起来,不过这样做没啥意义,这个是个啥题目吧,真正应用中不会这样的。
[code="java"]
public class ThreadTest {
public static void main(String[] args) {
Q q = new Q();
new Thread(new SaleS(q)).start();
new Thread(new SaleD(q)).start();
new Thread(new SaleS(q)).start();
new Thread(new SaleD(q)).start();
new Thread(new SaleS(q)).start();
}
}
class Q {
private int tickets = 10;
public synchronized int get() {
return tickets;
}
public synchronized void saleSingle() {
while (tickets > 0 && tickets % 2 == 0) {
try {
wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if (tickets > 0) {
System.out.println(Thread.currentThread().getName()
+ " is salling ticket " + tickets--);
} else {
System.out.println(Thread.currentThread().getName()
+ ": tickets has been salled over. ");
}
notifyAll();
}
public synchronized void saleDouble() {
while (tickets > 0 && tickets % 2 == 1) {
try {
wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if (tickets > 0) {
System.out.println(Thread.currentThread().getName()
+ " is salling ticket " + tickets--);
} else {
System.out.println(Thread.currentThread().getName()
+ ": tickets has been salled over. ");
}
notifyAll();
}
}
class SaleS implements Runnable {
Q q = null;
public SaleS(Q q) {
this.q = q;
}
public void run() {
while (q.get() >= 1) {
q.saleSingle();
}
}
}
class SaleD implements Runnable {
Q q = null;
public SaleD(Q q) {
this.q = q;
}
public void run() {
while (q.get() >= 2) {
q.saleDouble();
}
}
}
[/code]
为什么要多个线程呢?