写了一个简单的Java多线程的例子模拟火车售票,但线程同步后,只有一个线程在卖票了,不知道哪儿出问题了,请高手指出来?谢谢!//线程类
class SellThread implements Runnable {
int tickets = 100;
public void run() {
synchronized (this) {
while (tickets > 0) {
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}
}
}
}//测试类
public class TicketsSystem {
public static void main(String[] args) {
SellThread st = new SellThread();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}
}运行的结果是:只有第一个线程在卖票
class SellThread implements Runnable {
int tickets = 100;
public void run() {
synchronized (this) {
while (tickets > 0) {
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}
}
}
}//测试类
public class TicketsSystem {
public static void main(String[] args) {
SellThread st = new SellThread();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}
}运行的结果是:只有第一个线程在卖票
class SellThread implements Runnable {
int tickets = 100;
public void run() {
while(true){
if(tickets>0){
count();
}else{
break;
}
}
}
public void count(){
synchronized(this)
{
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}
}
} //测试类
public class TicketsSystem {
public static void main(String[] args) {
SellThread st = new SellThread();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}
}
//线程类
class SellThread implements Runnable {
int tickets = 100;
public void run() {
while(true){
if(tickets>0){
count();
}else{
break;
}
}
}
public void count(){
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}
} //测试类
public class TicketsSystem {
public static void main(String[] args) {
SellThread st = new SellThread();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}
}
public void count() {
synchronized (this) {
{
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
加个sleep看的更加的清楚一些,楼主把 run方法的方法体给同步了 以后在里面循环没完 其他的线程肯定是没法执行的啊
class SellThread implements Runnable {
int tickets = 100;
public void run() {
while(true){
if(tickets>0){
count();
}else{
break;
}
}
}
public void count(){
synchronized(this)
{
if(tickets>0){
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}else{
System.out.println("票卖完了!!");
}
}
}
} //测试类
public class TicketsSystem {
public static void main(String[] args) {
SellThread st = new SellThread();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}
}
while (tickets > 0) {
交换一下~~~~~~
一旦一个线程run起来后他一直占有对象锁直到运行到tickets<1释放对象锁
其他线程就算得到对象锁这时tickets>0已经不满足,当然不打印语句了
while (tickets > 0) {
synchronized (this) {
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}
}
}
class Demo1 implements Runnable
{
private int ticket = 10 ;
public void run()
{
while(this.ticket>0)
{
System.out.println("卖票:"+this.ticket--) ;
}
}
};
public class Test6
{
public static void main(String args[])
{
// 四个售票点应该控制同一个资源:10
Demo d = new Demo() ; Thread t1 = new Thread(d) ;
Thread t2 = new Thread(d) ;
Thread t3 = new Thread(d) ;
Thread t4 = new Thread(d) ;
t1.start() ;
t2.start() ;
t3.start() ;
t4.start() ;
}
};