代码如下,运行了几次,好像没有重复的,但我总觉得是不安全的,到底结果是怎样的呢?class Test { public static void main(String[] args) {
Runnable st = new SaleTickets();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}}class SaleTickets implements Runnable {
int cnt = 50; public void run() {
while (true) {
if (cnt < 1)
break;
System.out.println(Thread.currentThread().getName() + "'s ticket "
+ cnt--);
}
}
}

解决方案 »

  1.   

    才试了几次,那就试多几次喽import java.util.*;import java.util.ArrayList;import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
    public class Test {
        public static void main(String[] args) {
            Runnable st = new SaleTickets();
            new Thread(st).start();
            new Thread(st).start();
            new Thread(st).start();
            new Thread(st).start();
        }
    }
    class SaleTickets implements Runnable {
        static HashSet<Integer> integers = new HashSet<Integer>();
        int cnt = 5000;
        public void run() {
            while (true) {
                if (cnt < 1)
                    break;
                if(!integers.add(cnt)) {            
                    System.out.println("有冲突");;
                }
                cnt--;
            }
        }
    }
    /*output:
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突
    有冲突*/
      

  2.   

    static HashSet<Integer> integers = new HashSet<Integer>();
    这种声明方式不好,假设每个线程有自己的cnt的时候也会冲突不断。没必要用static类型。直接
    private HashSet<Integer> integers = new HashSet<Integer>();
    即可
      

  3.   

    static 不行的private的祝楼主好运
      

  4.   

    不是线程安全的
    可以考虑 volatile int cnt = 50;
      

  5.   


    这个真不行  注意下面的cnt--操作
      

  6.   

    一个修改方法class Test {    public static void main(String[] args) {
            Runnable st = new SaleTickets();
            new Thread(st).start();
            new Thread(st).start();
            new Thread(st).start();
            new Thread(st).start();
        }}class SaleTickets implements Runnable {
    private int cnt = 50;    public void run() {
            while (true) {
                if (cnt < 1)
                    break;
                System.out.println(Thread.currentThread().getName() + "'s ticket "
                        + modify());
            }
        }
        
        synchronized int modify(){
         return cnt--;
        }
    }
      

  7.   

    不安全!class Test {    public static void main(String[] args) {
            Runnable st = new SaleTickets();
            new Thread(st).start();
            new Thread(st).start();
            new Thread(st).start();
            new Thread(st).start();
        }}class SaleTickets implements Runnable {
        private int cnt = 50;    public void run() {
            //while (true) {
              while (cnt>1) {
                if (cnt < 1)//执行到这里有安全隐患!
                    //break;
                System.out.println(Thread.currentThread().getName() + "'s ticket "
                        + modify());
            }
        }
        
        synchronized int modify(){
            return cnt--;
        }
    }
      

  8.   

    安全,每个线程对象都有独立的变量tcn
      

  9.   

    确定是非安全的了。
    我将测试数据放大,5000条以上时,就能出现重复的情况。
    set集合可以检验,其实直接打印数字,复制到excel中也很方便的检验出来了。
    谢谢各位。