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

  1. public class ThreadDemo2 {
  2.         public static void main (String [] args){
  3.         Tickets tic = new Tickets();
  4.         Thread t1 = new Thread(tic);
  5.         Thread t2 = new Thread(tic);
  6.         Thread t3 = new Thread(tic);
  7.         Thread t4 = new Thread(tic);
  8.         t1.start();
  9.         t2.start();
  10.         t3.start();
  11.         t4.start();
  12.         System.out.println(Thread.currentThread().getName());
  13.         }

  14. }

  15. class Tickets implements Runnable{
  16.         private int tickets = 5000;
  17.         private Object obj = new Object();
  18.         public  void run(){
  19.                 while(true){
  20.                         synchronized (obj){
  21.                                 if(tickets > 0){
  22.                         }
  23.                                 try{Thread.sleep(1);}catch(InterruptedException e){}
  24.                                 System.out.println(Thread.currentThread().getName()+"..."+tickets--);
  25.                         }
  26.                         else {
  27.                                 return;
  28.                         }
  29.                 }
  30.                 }
  31.         }
  32.        
复制代码


在上述代码中,
  1. else {
  2.                                 return;
  3.                         }
复制代码

这句代码出现了语法错误,系统eclipse提醒删除该代码,但是我删除后发现,运行时处于无限循环状态,根本停不下了,但是如果加上这句代码,编译器又报语法错误,有点看不懂了,哪位师兄帮忙解决一下?谢谢了!

3 个回复

倒序浏览
你的代码相当于如下的代码
  1. public class Demo
  2. {
  3.         public static void main(String[] args) throws Exception
  4.         {
  5.                 Tickets tic = new Tickets();
  6.                 Thread t1 = new Thread(tic);
  7.                 Thread t2 = new Thread(tic);
  8.                 Thread t3 = new Thread(tic);
  9.                 Thread t4 = new Thread(tic);
  10.                 t1.start();
  11.                 t2.start();
  12.                 t3.start();
  13.                 t4.start();
  14.                 System.out.println(Thread.currentThread().getName());
  15.         }
  16. }

  17. class Tickets implements Runnable
  18. {
  19.         private int tickets = 5000;
  20.         private Object obj = new Object();

  21.         public void run()
  22.         {
  23.                 while (true)
  24.                 {
  25.                         synchronized (obj) {
  26.                                 if (tickets > 0)
  27.                                 {
  28.                                 }
  29.                                 try
  30.                                 {
  31.                                         Thread.sleep(1);
  32.                                 } catch (InterruptedException e)
  33.                                 {
  34.                                 }
  35.                                 System.out.println(Thread.currentThread().getName() + "..."
  36.                                                 + tickets--);
  37.                                 else
  38.                                 {
  39.                                         return ;
  40.                                 }
  41.                         }
  42.                 }
  43.         }
  44. }
复制代码


你的else语句上缺少一个判断语句。else语句是不能单独用的,但是if语句可以单独使用

例如
  1. if(true)
  2. {
  3.         System.out.println("我可以单独使用");
  4. }
复制代码



-----------------------------

  1. if(true)
  2. {
  3.         System.out.println("我可以单独使用");
  4. }
  5. else{
  6.         System.out.println("我需要结合if语句使用");
  7. }
复制代码


--------------------------
以下的格式是错误的:

  1. else
  2. {
  3.         System.out.println("我需要结合if语句使用");
  4. }
复制代码


--------------------------
还有,写代码的时候括号要写整齐一点,代码没有层次感看起来很容易混乱
回复 使用道具 举报
。。楼上在说什么,楼主的问题很简单。。把return 换成break;
那是循环,所以直接跳出就行了
回复 使用道具 举报
嘎路的米 发表于 2015-6-5 07:15
你的代码相当于如下的代码

感谢师兄回复,问题已解决,for语句处多了半个大括号,应将try{}catch语句放到for大括号之间!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马