计数器给出随机数

时间:2017-03-23 21:41:45

标签: java arrays multithreading counter

我正在尝试实施一项计划,让学生从1到10开始计算。

以下是我的代码:

主要课程:

public class JavaLab5 {
    public static final int DEBUG = 0;

    public static void multipleThreads(int number) {
        StudentThread []st = new StudentThread[number];
        for (int loop = 0; loop < number;loop++) {
            st[loop] = new StudentThread();
            st[loop].start();
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] {
        multipleThreads(10);  
    } 
}

学生班:

public class Student {
    static int studentCounter = 1;
    String name;
    private static int count = 0;
    public static int instances = 0;

    // Getters
    /**
     *
     * @return
     */
    public  String getName() {
        return this.name;
    }

    // Setters
    /**
     *
     * @param name
     */
    public void setName(String name) {
        if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
        this.name = name;
    }

    /**
     * Default constructor. 
     * Populates name with defaults
     */
    public Student() {
        instances++;
        this.name = "Not Set";
    }

    /** 
     * Constructor with parameters 
     * @param name String with the name
    */
    public Student(String name) {
        this.name = name;
    }

    /**
     * Destructor
     * @throws Throwable 
     */
    protected void finalize() throws Throwable {
        //do finalization here
        instances--;
        super.finalize(); //not necessary if extending Object.
    } 

    /**
     * @return 
     */
    public void getCounter() {
        count++;
        //return count;
        System.out.println(count);
    }

    @Override
    public String toString () {
        return this.name; 
    }

    /**
     *
     * @return
     */
    public String getSubjects() {
        return null;  
    }
}

计算机学生班:

public class ComputerStudent extends Student {
    int studCountObj;

    /**
     * Default constructor
     */
    public ComputerStudent() {
        super();
    }

    public ComputerStudent(String name) {
        super(name);
        studCountObj=studentCounter;
        studentCounter++;
    }

    /**
     * Display information about the subject
     * @return 
     */
    @Override
    public String getSubjects(){
        return(" Computer Student" + "[" + studCountObj + "] " + name + ": "); 
    }
}

理科学生:

public class ScienceStudent extends Student {
    int studCountObj;

    /**
     * Default constructor
     */
    public ScienceStudent() {
        super();
    }

    public ScienceStudent(String name) {
        super(name);
        studCountObj=studentCounter;
        studentCounter++;
    }

    /**
     * Display information about the subject
     * @return 
     */
    @Override
    public String getSubjects(){
        return(" Science Student" + "[" + studCountObj + "]" + "-" + name + ": ");
    } 
}

数学学生:

public class MathStudent extends Student {
    int studCountObj;

    /**
     * Default constructor
     * @param name
     */
    public MathStudent(String name) {
        super(name);
        studCountObj=studentCounter;
        studentCounter++;
    }

    public MathStudent() {
        super();
    }

    /**
     * Display information about the subject
     * @return 
     */
    @Override
    public String getSubjects(){
        return(" MathStudent" + "[" + studCountObj + "]" + "-" + name + ": ");
    }
}

学生主题:

public class StudentThread extends Thread {

    @Override
    public void run(){
        Student s[] = new Student[10];

        //Student Array containing different types of students
        s[0] = new MathStudent("Smith");
        s[1] = new MathStudent("Jack");
        s[2] = new MathStudent("Victor");
        s[3] = new MathStudent("Mike");
        s[4] = new ScienceStudent("Dave");
        s[5] = new ScienceStudent("Oscar");
        s[6] = new ScienceStudent("Peter");
        s[7] = new ComputerStudent("Philip");
        s[8] = new ComputerStudent("Shaun");
        s[9] = new ComputerStudent("Scott");

        //Checking and looping till the count reaches 10
        //for (int j = 0; j < 10; j++) { 
            //Checking and looping till the end of the student array
            for (int i = 0; i < s.length; i++) {
                System.out.println();
                System.out.print(s[i].getSubjects() + " - " + "Count:");
                s[i].getCounter();
        }
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        System.out.println("Exception at StudentThread.run: " + e);
        }
        }

}

当前代码的输出给出:

MathStudent[12]-Smith:  - Count:
 MathStudent[28]-Smith:  - Count:2




 MathStudent[1]-Smith:  - Count:3




 MathStudent[20]-Smith:  - Count:4

 MathStudent[21]-Jack:  - Count:5

 MathStudent[16]-Smith:  - Count:6

 MathStudent[2]-Jack:  - Count:7

 MathStudent[36]-Smith:  - Count:8

 MathStudent[24]-Smith:  - Count:9

 MathStudent[25]-Jack:  - Count:10

 MathStudent[26]-Victor:  - Count:11

 MathStudent[27]-Mike:  - Count:12

 Science Student[52]-Dave:  - Count:13
 MathStudent[37]-Jack:  - Count:14

 MathStudent[38]-Victor:  - Count:15

 MathStudent[39]-Mike:  - Count: MathStudent[29]-Jack:  - Count:17

1

 MathStudent[13]-Jack:  - Count:18

 MathStudent[14]-Victor:  - Count:19

 MathStudent[30]-Victor:  - Count:20

 MathStudent[32]-Smith:  - Count:21

 MathStudent[1]-Smith:  - Count:22

 MathStudent[33]-Jack:  - Count:23

 MathStudent[31]-Mike:  - Count:24

 MathStudent[15]-Mike:  - Count:25

 Science Student[64]-Dave:  - Count:26

 Science Student[65]-Oscar:  - Count:27

 Science Student[66]-Peter:  - Count:28

16
 Computer Student[67] Philip:  - Count:29


 MathStudent[4]-Victor:  - Count:30

 MathStudent[17]-Jack:  - Count:31

 MathStudent[18]-Victor:  - Count:32

 MathStudent[19]-Mike:  - Count:33

 MathStudent[22]-Victor:  - Count:34

 MathStudent[8]-Smith:  - Count:35

 MathStudent[9]-Jack:  - Count:36

 MathStudent[10]-Victor:  - Count:37

 MathStudent[11]-Mike:  - Count:38

 MathStudent[23]-Mike:  - Count:39

 Science Student[61]-Dave:  - Count:40

 MathStudent[6]-Mike:  - Count:41

 Science Student[46]-Dave:  - Count:42

 Science Student[53]-Oscar:  - Count:43

 Computer Student[70] Shaun:  - Count:44

 Computer Student[73] Scott:  - Count:45

 Science Student[55]-Dave:  - Count:46

 Science Student[56]-Oscar:  - Count:47

 MathStudent[34]-Victor:  - Count:48

 MathStudent[35]-Mike:  - Count:49

 Science Student[41]-Dave:  - Count:50

 MathStudent[3]-Jack:  - Count:51

 Science Student[43]-Oscar:  - Count:52

 Science Student[44]-Peter:  - Count:53

 Science Student[57]-Peter:  - Count:54

 Science Student[49]-Dave:  - Count:55

 Science Student[50]-Oscar:  - Count:56

 Science Student[51]-Peter:  - Count:57

 Computer Student[97] Philip:  - Count:58

 Computer Student[98] Shaun:  - Count:59

 Computer Student[99] Scott:  - Count:60
 Science Student[54]-Peter:  - Count:61

 Computer Student[88] Philip:  - Count:62

 Computer Student[89] Shaun:  - Count:63

 Computer Student[90] Scott:  - Count:64
 Science Student[47]-Oscar:  - Count:65

 Science Student[48]-Peter:  - Count:66

 Computer Student[76] Philip:  - Count:67

 Computer Student[77] Shaun:  - Count:68

 Computer Student[78] Scott:  - Count:69
 Science Student[62]-Oscar:  - Count:70

 Science Student[63]-Peter:  - Count:71

 Computer Student[79] Philip:  - Count:72

 Computer Student[80] Shaun:  - Count:73

 Science Student[58]-Dave:  - Count:74

 Science Student[59]-Oscar:  - Count:75

 Science Student[60]-Peter:  - Count:76

 Science Student[68]-Dave:  - Count:77

 Computer Student[82] Philip:  - Count:78

 Computer Student[81] Scott:  - Count:79
 Computer Student[85] Philip:  - Count:80

 Computer Student[86] Shaun:  - Count:81

 Computer Student[94] Philip:  - Count:82

 MathStudent[5]-Victor:  - Count:83

 Computer Student[95] Shaun:  - Count:84

 Computer Student[87] Scott:  - Count:85
 Computer Student[83] Shaun:  - Count:86

 Science Student[69]-Oscar:  - Count:87

 Computer Student[84] Scott:  - Count:88
 Computer Student[96] Scott:  - Count:89
 MathStudent[7]-Mike:  - Count:90

 Science Student[71]-Peter:  - Count:91

 Science Student[40]-Dave:  - Count:92

 Science Student[42]-Oscar:  - Count:93

 Computer Student[72] Philip:  - Count:94

 Science Student[45]-Peter:  - Count:95

 Computer Student[91] Philip:  - Count:96

 Computer Student[74] Shaun:  - Count:97

 Computer Student[92] Shaun:  - Count:98

 Computer Student[93] Scott:  - Count:99
 Computer Student[75] Scott:  - Count:100

正如您所看到的,当前输出给出了一个randon计数值和随机学生值,我不明白为什么。

我需要的输出是:

 MathStudent[1]-Smith:  - Count:1
 MathStudent[2]-Jack:  - Count:1
 MathStudent[3]-Victor:  - Count:1
 MathStudent[4]-Mike:  - Count:1
 Science Student[5]-Dave:  - Count:1
 Science Student[6]-Oscar:  - Count:1
 Science Student[7]-Peter:  - Count:1
 Computer Student[8] Philip:  - Count:1
 Computer Student[9] Shaun:  - Count:1
 Computer Student[10] Scott:  - Count:1
 MathStudent[1]-Smith:  - Count:2
 MathStudent[2]-Jack:  - Count:2
 MathStudent[3]-Victor:  - Count:2
 MathStudent[4]-Mike:  - Count:2
 Science Student[5]-Dave:  - Count:2
 Science Student[6]-Oscar:  - Count:2
 Science Student[7]-Peter:  - Count:2
 Computer Student[8] Philip:  - Count:2
 Computer Student[9] Shaun:  - Count:2
 Computer Student[10] Scott:  - Count:2
 MathStudent[1]-Smith:  - Count:3
 MathStudent[2]-Jack:  - Count:3
 MathStudent[3]-Victor:  - Count:3
 MathStudent[4]-Mike:  - Count:3
 Science Student[5]-Dave:  - Count:3
 Science Student[6]-Oscar:  - Count:3
 Science Student[7]-Peter:  - Count:3
 Computer Student[8] Philip:  - Count:3
 Computer Student[9] Shaun:  - Count:3
 Computer Student[10] Scott:  - Count:3
 MathStudent[1]-Smith:  - Count:4
 MathStudent[2]-Jack:  - Count:4
 MathStudent[3]-Victor:  - Count:4
 MathStudent[4]-Mike:  - Count:4
 Science Student[5]-Dave:  - Count:4
 Science Student[6]-Oscar:  - Count:4
 Science Student[7]-Peter:  - Count:4
 Computer Student[8] Philip:  - Count:4
 Computer Student[9] Shaun:  - Count:4
 Computer Student[10] Scott:  - Count:4
 MathStudent[1]-Smith:  - Count:5
 MathStudent[2]-Jack:  - Count:5
 MathStudent[3]-Victor:  - Count:5
 MathStudent[4]-Mike:  - Count:5
 Science Student[5]-Dave:  - Count:5
 Science Student[6]-Oscar:  - Count:5
 Science Student[7]-Peter:  - Count:5
 Computer Student[8] Philip:  - Count:5
 Computer Student[9] Shaun:  - Count:5
 Computer Student[10] Scott:  - Count:5
 MathStudent[1]-Smith:  - Count:6
 MathStudent[2]-Jack:  - Count:6
 MathStudent[3]-Victor:  - Count:6
 MathStudent[4]-Mike:  - Count:6
 Science Student[5]-Dave:  - Count:6
 Science Student[6]-Oscar:  - Count:6
 Science Student[7]-Peter:  - Count:6
 Computer Student[8] Philip:  - Count:6
 Computer Student[9] Shaun:  - Count:6
 Computer Student[10] Scott:  - Count:6
 MathStudent[1]-Smith:  - Count:7
 MathStudent[2]-Jack:  - Count:7
 MathStudent[3]-Victor:  - Count:7
 MathStudent[4]-Mike:  - Count:7
 Science Student[5]-Dave:  - Count:7
 Science Student[6]-Oscar:  - Count:7
 Science Student[7]-Peter:  - Count:7
 Computer Student[8] Philip:  - Count:7
 Computer Student[9] Shaun:  - Count:7
 Computer Student[10] Scott:  - Count:7
 MathStudent[1]-Smith:  - Count:8
 MathStudent[2]-Jack:  - Count:8
 MathStudent[3]-Victor:  - Count:8
 MathStudent[4]-Mike:  - Count:8
 Science Student[5]-Dave:  - Count:8
 Science Student[6]-Oscar:  - Count:8
 Science Student[7]-Peter:  - Count:8
 Computer Student[8] Philip:  - Count:8
 Computer Student[9] Shaun:  - Count:8
 Computer Student[10] Scott:  - Count:8
 MathStudent[1]-Smith:  - Count:9
 MathStudent[2]-Jack:  - Count:9
 MathStudent[3]-Victor:  - Count:9
 MathStudent[4]-Mike:  - Count:9
 Science Student[5]-Dave:  - Count:9
 Science Student[6]-Oscar:  - Count:9
 Science Student[7]-Peter:  - Count:9
 Computer Student[8] Philip:  - Count:9
 Computer Student[9] Shaun:  - Count:9
 Computer Student[10] Scott:  - Count:9
 MathStudent[1]-Smith:  - Count:10
 MathStudent[2]-Jack:  - Count:10
 MathStudent[3]-Victor:  - Count:10
 MathStudent[4]-Mike:  - Count:10
 Science Student[5]-Dave:  - Count:10
 Science Student[6]-Oscar:  - Count:10
 Science Student[7]-Peter:  - Count:10
 Computer Student[8] Philip:  - Count:10
 Computer Student[9] Shaun:  - Count:10
 Computer Student[10] Scott:  - Count:10

为什么输出打印出随机计数值和随机学生数以及我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

count和StudentCounter是静态变量。因此,它们由所有学生对象更新。你开始10,并在每个线程中实现10个学生。这100个学生实例中的每一个都更新这些变量,这些变量在所有实例之间共享,因为它们被声明为静态。

答案 1 :(得分:0)

对于这种问题,Java提供了AtomicInteger,以原子方式执行Integer操作。

只需替换AtomicInteger的静态int变量,然后使用类的方法:

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html