A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 821728010 中级黑马   /  2013-5-26 00:44  /  1482 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 821728010 于 2013-5-29 17:36 编辑

刚刚看了多线程,试着写了下代码,为什么只有一个线程在卖票?我都搞到100张票了,其他线程还抢不到么?

import java.lang.*;
import org.omg.CORBA.Current;
public class Demo2 {
        public static void main(String[] args) {
                Tiket ti = new Tiket();
                Thread th1 = new Thread(ti);
                Thread th2 = new Thread(ti);
                Thread th3 = new Thread(ti);
                Thread th4 = new Thread(ti);
                th1.start();
                th2.start();
                th3.start();
                th4.start();
}
        }
class Tiket implements Runnable{
        int ticket = 100;
        public void run(){
               
         synchronized (this) {
                while(true){
                         synchronized (this) {
                          if(ticket>0){
                             System.out.println(Thread.currentThread()+"第"+ticket+"张票");
                             ticket--;
                   }else{
                         break;
                            }
                          }
                         }
                }
         
        }
        }

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

3 个回复

倒序浏览
  1. public class Demo {
  2.         public static void main(String[] args) {
  3.                 Tiket ti = new Tiket();
  4.                 Thread th1 = new Thread(ti);
  5.                 Thread th2 = new Thread(ti);
  6.                 Thread th3 = new Thread(ti);
  7.                 Thread th4 = new Thread(ti);
  8.                 th1.start();
  9.                 th2.start();
  10.                 th3.start();
  11.                 th4.start();
  12.         }
  13. }

  14. class Tiket implements Runnable {
  15.         int ticket = 100;

  16.         public void run() {

  17.                 //synchronized (this) {                //同步代码块要加对地方
  18.                         while (true) {
  19.                                 synchronized (this) {
  20.                                         if (ticket > 0) {
  21. //                                                try {                                                                                                               
  22. //                                                        Thread.sleep(100);                        //睡眠下 方面看执行情况
  23. //                                                } catch (InterruptedException e) {
  24. //                                                        e.printStackTrace();
  25. //                                                }
  26.                                                 System.out.println(Thread.currentThread() + "第"
  27.                                                                 + ticket + "张票");
  28.                                                 ticket--;
  29.                                         } else {
  30.                                                 break;
  31.                                         }
  32.                                 }
  33.                         }
  34.                 //}

  35.         }
  36. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

回复 使用道具 举报
 同步的前提:1,必须要有两个或者两个以上的线程;2,必须是多个线程使用同一个锁。 你把synchronized加到while前面就相当于是单线程了

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1 很给力!

查看全部评分

回复 使用道具 举报
  1. import java.lang.*;
  2. import org.omg.CORBA.Current;
  3. public class Demo2 {
  4.         public static void main(String[] args) {
  5.                 Tiket ti = new Tiket();
  6.                 Thread th1 = new Thread(ti);
  7.                 Thread th2 = new Thread(ti);
  8.                 Thread th3 = new Thread(ti);
  9.                 Thread th4 = new Thread(ti);
  10.                 th1.start();
  11.                 th2.start();
  12.                 th3.start();
  13.                 th4.start();
  14. }
  15.         }
  16. class Tiket implements Runnable{
  17.         int ticket = 100;
  18.         public void run(){
  19.                
  20.          synchronized (this) { //这个地方不用加锁的,一加锁,就在这里一直循环,直到卖完才退出
  21.                 while(true){
  22.                          synchronized (this) {
  23.                           if(ticket>0){
  24.                              System.out.println(Thread.currentThread()+"第"+ticket+"张票");
  25.                              ticket--;
  26.                    }else{
  27.                          break;
  28.                             }
  29.                           }
  30.                          }
  31.                 }
  32.          
  33.         }
  34.         }
复制代码
黑马云青年为你解答
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马