62,614
社区成员
发帖
与我相关
我的任务
分享
public class Ticket {
private int ticketNum = 100;
public synchronized void sellTicket() {
ticketNum --;
}
public synchronized int getTicketNum() {
return ticketNum;
}
}
public class BuyTicket implements Runnable {
private Ticket ticket;
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
@Override
public void run() {
while(ticket.getTicketNum()>0){
ticket.sellTicket();
System.out.println(Thread.currentThread().getName() + "卖了一张票剩余"+ticket.getTicketNum()+"张票" );
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Ticket ticket = new Ticket();
BuyTicket buyTicket = new BuyTicket();
buyTicket.setTicket(ticket);
Thread Thread1 = new Thread(buyTicket,"窗口1");
Thread Thread2 = new Thread(buyTicket,"窗口2");
Thread Thread3 = new Thread(buyTicket,"窗口3");
Thread1.start();
Thread2.start();
Thread3.start();
}
}
public synchronized int sellTicket() {
ticketNum --;
return ticketNum;
}
然后改下run方法
while(ticket.getTicketNum()>0){
System.out.println(Thread.currentThread().getName() + "卖了一张票剩余"+ticket.sellTicket()+"张票" );
}
这样应该就可以了。
个人感觉查询票数的方法getTicketNum没必要定义为同步方法,因为在这个方法里没有修改ticketNum。
public class SellTiketDemo {
public static void main(String[] args) {
Ticket ticket = new Ticket();
BuyTicket buyTicket = new BuyTicket();
buyTicket.setTicket(ticket);
new Thread(buyTicket,"窗口1").start();;
new Thread(buyTicket,"窗口2").start();;
new Thread(buyTicket,"窗口3").start();;
}
}
class Ticket {
private int ticketNum = 1000;
public void sellTicket() {
ticketNum--;
}
public int getTicketNum() {
return ticketNum;
}
}
class BuyTicket implements Runnable {
private Ticket ticket;
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
@Override
public void run() {
while(true){ //线程开始执行之后,先进入循环,再执行同步代码。这样避免了数据读写不同步的现象。
synchronized (ticket) {
if(ticket.getTicketNum()>0){
ticket.sellTicket();
System.out.println(Thread.currentThread().getName() + "卖了一张票剩余"+ticket.getTicketNum()+"张票" );
}
else {
break;
}
}
}
}
}
public class SellTiketDemo {
public static void main(String[] args) {
Ticket ticket = new Ticket();
BuyTicket buyTicket = new BuyTicket();
buyTicket.setTicket(ticket);
new Thread(buyTicket,"窗口1").start();;
new Thread(buyTicket,"窗口2").start();;
new Thread(buyTicket,"窗口3").start();;
}
}
class Ticket {
private int ticketNum = 1000;
Object lockObject=new Object();
public void sellTicket() {
synchronized (lockObject) {
ticketNum--;
System.out.println(Thread.currentThread().getName() + "卖了一张票剩余"+this.getTicketNum()+"张票" );
}
}
public int getTicketNum() {
synchronized (lockObject) {
return ticketNum;
}
}
}
class BuyTicket implements Runnable {
private Ticket ticket;
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
@Override
public void run() {
while(ticket.getTicketNum()>0){
ticket.sellTicket();
}
}
}
public class SellTiketDemo {
public static void main(String[] args) {
Ticket ticket = new Ticket();
BuyTicket buyTicket = new BuyTicket();
buyTicket.setTicket(ticket);
Thread Thread1 = new Thread(buyTicket,"窗口1");
Thread Thread2 = new Thread(buyTicket,"窗口2");
Thread Thread3 = new Thread(buyTicket,"窗口3");
Thread1.start();
Thread2.start();
Thread3.start();
}
}
class Ticket {
private int ticketNum = 1000;
public void sellTicket() {
ticketNum--;
System.out.println(Thread.currentThread().getName() + "卖了一张票剩余"+this.getTicketNum()+"张票" );
}
public int getTicketNum() {
return ticketNum;
}
}
class BuyTicket implements Runnable {
private Ticket ticket;
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
@Override
public synchronized void run() {
while(ticket.getTicketNum()>0){
ticket.sellTicket();
}
}
} while (ticket.getTicketNum() > 0)
{
synchronized(ticket)
{
ticket.sellTicket();
System.out.println(Thread.currentThread().getName()
+ "卖了一张票,剩余" + ticket.getTicketNum() + "张票");
}
}