62,621
社区成员
发帖
与我相关
我的任务
分享
public class Ticket extends Thread {
private int ticket = 5;
@Override
public void run() {
// synchronized (this) {
while (true) {
if (ticket > 0) {
System.out.println("顾客到达 "
+ Thread.currentThread().getName() + "售票台");
System.out.println("售票台" + Thread.currentThread().getName()
+ "准备出售第" + ticket + "张票");
try {// 具体卖票过程
System.out.println("售票台需要5分钟出售这张票");
for (int i = 1; i < 5; i++) {
Thread.sleep(1000);
System.out.println(i + "分钟");
}
System.out.println("售票台"
+ Thread.currentThread().getName() + "卖完第"
+ ticket + "张票");
System.out.println("顾客从"
+ Thread.currentThread().getName() + "买到第"
+ ticket + "张票");
--ticket;
} catch (InterruptedException e) {
System.out.println("卖票不成功");
System.exit(1);
}
} else {
break;
}
}
// }
}
}
public static void main(String[] args) {
Ticket t = new Ticket();
Thread s = new Thread(t);
Thread s1 = new Thread(t);
Thread s2 = new Thread(t);
Thread s3 = new Thread(t);
s.start();
s1.start();
s2.start();
s3.start();
}
public class Test4 {
public static void main(String[] args) {
TestThread4 t = new TestThread4();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
class TestThread4 extends Thread {
private int tick =20;
public void run() {
while (true) {
if (tick > 0) {
System.out.println(Thread.currentThread().getName() + "出售票"
+ tick--);
} else {
break;
}
}
}
}
class Ticket extends Thread {
private volatile int ticket = 5;
private volatile int ticketControl = ticket;
@Override
public void run() {
while (true) {
if (ticketControl-- > 0) {
System.out.println("顾客到达 "
+ Thread.currentThread().getName() + "售票台");
System.out.println("售票台" + Thread.currentThread().getName()
+ "准备出售第" + ticket + "张票");
try {// 具体卖票过程
System.out.println("售票台需要5分钟出售这张票");
for (int i = 1; i < 5; i++) {
Thread.sleep(100);
System.out.println(i + "分钟");
}
System.out.println("售票台"
+ Thread.currentThread().getName() + "卖完第"
+ ticket + "张票");
System.out.println("顾客从"
+ Thread.currentThread().getName() + "买到第"
+ ticket + "张票");
--ticket;
} catch (InterruptedException e) {
System.out.println("卖票不成功");
System.exit(1);
}
} else {
break;
}
}
}
}
[/quote]
恩,还没试,但是逻辑上应该正确,,但是你在if里加了个--,下面的--ticket就可以去掉了。
你还是太粗心,O(∩_∩)O哈哈~[/quote]
我用了两个参数呢,两个是不同的!你跑一下就知道了,如果只用一个的话,会冲突,出现负数的票数。用两个就不会了!所以--ticket是要的
class Ticket extends Thread {
private volatile int ticket = 5;
private volatile int ticketControl = ticket;
@Override
public void run() {
while (true) {
if (ticketControl-- > 0) {
System.out.println("顾客到达 "
+ Thread.currentThread().getName() + "售票台");
System.out.println("售票台" + Thread.currentThread().getName()
+ "准备出售第" + ticket + "张票");
try {// 具体卖票过程
System.out.println("售票台需要5分钟出售这张票");
for (int i = 1; i < 5; i++) {
Thread.sleep(100);
System.out.println(i + "分钟");
}
System.out.println("售票台"
+ Thread.currentThread().getName() + "卖完第"
+ ticket + "张票");
System.out.println("顾客从"
+ Thread.currentThread().getName() + "买到第"
+ ticket + "张票");
--ticket;
} catch (InterruptedException e) {
System.out.println("卖票不成功");
System.exit(1);
}
} else {
break;
}
}
}
}
[/quote]
恩,还没试,但是逻辑上应该正确,,但是你在if里加了个--,下面的--ticket就可以去掉了。
你还是太粗心,O(∩_∩)O哈哈~
int nTicket = 0;
synchronized(this) {
nTicket = ticketControl--;
}
if (nTicket > 0) {
class Ticket extends Thread {
private volatile int ticket = 5;
private volatile int ticketControl = ticket;
@Override
public void run() {
while (true) {
if (ticketControl-- > 0) {
System.out.println("顾客到达 "
+ Thread.currentThread().getName() + "售票台");
System.out.println("售票台" + Thread.currentThread().getName()
+ "准备出售第" + ticket + "张票");
try {// 具体卖票过程
System.out.println("售票台需要5分钟出售这张票");
for (int i = 1; i < 5; i++) {
Thread.sleep(100);
System.out.println(i + "分钟");
}
System.out.println("售票台"
+ Thread.currentThread().getName() + "卖完第"
+ ticket + "张票");
System.out.println("顾客从"
+ Thread.currentThread().getName() + "买到第"
+ ticket + "张票");
--ticket;
} catch (InterruptedException e) {
System.out.println("卖票不成功");
System.exit(1);
}
} else {
break;
}
}
}
}
public class Ticket implements Runnable {
private int ticket = 5;
public void run() {
while (true)
synchronized (this)
{
if (ticket > 0)
{
System.out.println("顾客到达 "
+ Thread.currentThread().getName() + "售票台");
System.out.println("售票台" + Thread.currentThread().getName()
+ "准备出售第" + ticket + "张票");
try
{// 具体卖票过程
/*System.out.println("售票台需要5分钟出售这张票");
for (int i = 1; i < 5; i++) {
Thread.sleep(1000);
System.out.println(i + "分钟");
}*/
System.out.println("售票台"
+ Thread.currentThread().getName() + "卖完第"
+ ticket + "张票");
System.out.println("顾客从"
+ Thread.currentThread().getName() + "买到第"
+ ticket + "张票");
ticket--;
}
catch (Exception e)
{
System.out.println("卖票不成功");
System.exit(1);
}
}
else
{
break;
}
}
}
public static void main(String[] args) {
Ticket t = new Ticket();
Thread s = new Thread(t);
Thread s1 = new Thread(t);
Thread s2 = new Thread(t);
Thread s3 = new Thread(t);
s.start();
s1.start();
s2.start();
s3.start();
}
}
稍做了一下修改 这个是同步问题 错票 负票都是没上锁的原因