练习: 抢火车票; 成都 -> 宜宾 有1000张票,有4个窗口同时卖票 , 同步方法 ,同步代码块,线程的常用方法, 继承Thread类,实现Runnable接口,使用java多线程相关知识写出代码,不要写太复杂,并且每一行带有注释,,
时间: 2024-05-09 11:15:32 浏览: 73
在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口(csdn)————程.pdf
使用同步方法:
```java
public class Ticket implements Runnable {
private int count = 1000; // 火车票总数
private static int window = 4; // 售票窗口数量
// 同步方法,保证线程安全
public synchronized void sellTicket() {
// 判断是否还有票
if (count > 0) {
System.out.println(Thread.currentThread().getName() + "售出第" + (1000 - count + 1) + "张票");
count--;
}
}
@Override
public void run() {
while (count > 0) {
sellTicket();
}
}
public static void main(String[] args) {
Ticket ticket = new Ticket();
// 创建4个售票窗口
Thread[] threads = new Thread[window];
for (int i = 0; i < window; i++) {
threads[i] = new Thread(ticket, "窗口" + (i + 1));
threads[i].start();
}
}
}
```
使用同步代码块:
```java
public class Ticket implements Runnable {
private int count = 1000; // 火车票总数
private static int window = 4; // 售票窗口数量
private Object lock = new Object(); // 同步锁
@Override
public void run() {
while (count > 0) {
synchronized (lock) { // 同步代码块,保证线程安全
// 判断是否还有票
if (count > 0) {
System.out.println(Thread.currentThread().getName() + "售出第" + (1000 - count + 1) + "张票");
count--;
}
}
}
}
public static void main(String[] args) {
Ticket ticket = new Ticket();
// 创建4个售票窗口
Thread[] threads = new Thread[window];
for (int i = 0; i < window; i++) {
threads[i] = new Thread(ticket, "窗口" + (i + 1));
threads[i].start();
}
}
}
```
线程的常用方法:
```java
public class Ticket extends Thread {
private int count = 1000; // 火车票总数
private static int window = 4; // 售票窗口数量
@Override
public void run() {
while (count > 0) {
// 判断是否还有票
if (count > 0) {
System.out.println(Thread.currentThread().getName() + "售出第" + (1000 - count + 1) + "张票");
count--;
}
try {
sleep(100); // 线程休眠100毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Ticket ticket = new Ticket();
// 创建4个售票窗口
Thread[] threads = new Thread[window];
for (int i = 0; i < window; i++) {
threads[i] = new Thread(ticket, "窗口" + (i + 1));
threads[i].start();
}
// 等待所有线程执行完毕
for (int i = 0; i < window; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
继承Thread类:
```java
public class Ticket extends Thread {
private int count = 1000; // 火车票总数
@Override
public void run() {
while (count > 0) {
// 判断是否还有票
if (count > 0) {
System.out.println(getName() + "售出第" + (1000 - count + 1) + "张票");
count--;
}
try {
sleep(100); // 线程休眠100毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 创建4个售票窗口
Ticket[] tickets = new Ticket[4];
for (int i = 0; i < 4; i++) {
tickets[i] = new Ticket();
tickets[i].setName("窗口" + (i + 1));
tickets[i].start();
}
// 等待所有线程执行完毕
for (int i = 0; i < 4; i++) {
try {
tickets[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
实现Runnable接口:
```java
public class Ticket implements Runnable {
private int count = 1000; // 火车票总数
@Override
public void run() {
while (count > 0) {
// 判断是否还有票
if (count > 0) {
System.out.println(Thread.currentThread().getName() + "售出第" + (1000 - count + 1) + "张票");
count--;
}
try {
Thread.sleep(100); // 线程休眠100毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 创建4个售票窗口
Ticket ticket = new Ticket();
Thread[] threads = new Thread[4];
for (int i = 0; i < 4; i++) {
threads[i] = new Thread(ticket, "窗口" + (i + 1));
threads[i].start();
}
// 等待所有线程执行完毕
for (int i = 0; i < 4; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
阅读全文