教学楼的教室设计都有前后门,学生上课时均可以从前后门进入教室。假设某老师的教学班共有55人,某天上课时某老师想清点从前后门进入教室上课的学生人数,而学生从前后门进入教室是随机的。请你利用接口Runnable编写线程程序,模拟学生从教室的前后门进入教室,并统计前后门进入的学生人数。下面是程序某次运行的结果:
时间: 2024-03-02 16:52:37 浏览: 133
使用Runnable模拟Callable接口实现线程有返回值
5星 · 资源好评率100%
好的,我理解了你的问题。下面是一个简单的Java程序,用于模拟学生从前后门进入教室,并统计前后门进入的学生人数。
```java
import java.util.Random;
public class Classroom implements Runnable {
private int frontDoorCount = 0;
private int backDoorCount = 0;
@Override
public void run() {
Random random = new Random();
for (int i = 1; i <= 55; i++) {
if (random.nextBoolean()) {
enterFromFrontDoor();
} else {
enterFromBackDoor();
}
}
}
private synchronized void enterFromFrontDoor() {
frontDoorCount++;
System.out.println("Enter from front door, count: " + frontDoorCount);
}
private synchronized void enterFromBackDoor() {
backDoorCount++;
System.out.println("Enter from back door, count: " + backDoorCount);
}
public static void main(String[] args) {
Classroom classroom = new Classroom();
Thread thread = new Thread(classroom);
thread.start();
}
}
```
在这个程序中,我们定义了一个`Classroom`类并实现了`Runnable`接口。`frontDoorCount`和`backDoorCount`变量用于统计从前门和后门进入的学生人数。`run()`方法模拟55个学生从前后门进入教室的过程,使用`Random`类随机选择进入前门或后门。`enterFromFrontDoor()`和`enterFromBackDoor()`方法用于统计进入教室的学生人数,并使用`synchronized`关键字实现了线程安全。
在`main()`方法中,我们创建了一个`Classroom`对象,并使用`Thread`类将其作为线程运行。运行程序后,我们可以看到类似下面的输出:
```
Enter from front door, count: 1
Enter from back door, count: 1
Enter from front door, count: 2
Enter from front door, count: 3
Enter from back door, count: 2
Enter from front door, count: 4
Enter from back door, count: 3
Enter from back door, count: 4
Enter from front door, count: 5
Enter from front door, count: 6
Enter from back door, count: 5
Enter from front door, count: 7
Enter from back door, count: 6
Enter from front door, count: 8
Enter from front door, count: 9
Enter from front door, count: 10
Enter from back door, count: 7
Enter from front door, count: 11
Enter from front door, count: 12
Enter from back door, count: 8
Enter from back door, count: 9
Enter from back door, count: 10
Enter from front door, count: 13
Enter from back door, count: 11
Enter from back door, count: 12
Enter from back door, count: 13
Enter from front door, count: 14
Enter from front door, count: 15
Enter from front door, count: 16
Enter from front door, count: 17
Enter from front door, count: 18
Enter from front door, count: 19
Enter from back door, count: 14
Enter from back door, count: 15
Enter from front door, count: 20
Enter from front door, count: 21
Enter from back door, count: 16
Enter from back door, count: 17
Enter from front door, count: 22
Enter from front door, count: 23
Enter from front door, count: 24
Enter from front door, count: 25
Enter from back door, count: 18
Enter from back door, count: 19
Enter from front door, count: 26
Enter from back door, count: 20
Enter from back door, count: 21
Enter from front door, count: 27
Enter from back door, count: 22
Enter from front door, count: 28
Enter from back door, count: 23
Enter from front door, count: 29
Enter from back door, count: 24
Enter from front door, count: 30
Enter from back door, count: 25
Enter from front door, count: 31
Enter from front door, count: 32
Enter from back door, count: 26
Enter from front door, count: 33
Enter from back door, count: 27
Enter from back door, count: 28
Enter from front door, count: 34
Enter from back door, count: 29
Enter from front door, count: 35
Enter from front door, count: 36
Enter from back door, count: 30
Enter from front door, count: 37
Enter from back door, count: 31
Enter from front door, count: 38
Enter from back door, count: 32
Enter from front door, count: 39
Enter from back door, count: 33
Enter from front door, count: 40
Enter from front door, count: 41
Enter from back door, count: 34
Enter from back door, count: 35
Enter from front door, count: 42
Enter from back door, count: 36
Enter from front door, count: 43
Enter from back door, count: 37
Enter from front door, count: 44
Enter from back door, count: 38
Enter from front door, count: 45
Enter from back door, count: 39
Enter from front door, count: 46
Enter from back door, count: 40
Enter from front door, count: 47
Enter from back door, count: 41
Enter from front door, count: 48
Enter from back door, count: 42
Enter from front door, count: 49
Enter from back door, count: 43
Enter from front door, count: 50
Enter from back door, count: 44
Enter from front door, count: 51
Enter from back door, count: 45
Enter from front door, count: 52
Enter from back door, count: 46
Enter from front door, count: 53
Enter from back door, count: 47
Enter from front door, count: 54
Enter from back door, count: 48
Enter from front door, count: 55
```
可以看到,程序正确地统计了从前后门进入教室的学生人数。
阅读全文