JAVA多线程编程小实验
【摘要】 实验要求:(1)掌握两种创建线程的方法;(2)掌握线程同步的方法。实验内容:(1)编程模拟售票系统,模拟多个窗口(至少4个)同时出售100张车票的情况;用实现Runnable接口的方法实现多线程。import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class...
实验要求:
(1)掌握两种创建线程的方法;
(2)掌握线程同步的方法。
实验内容:
(1)编程模拟售票系统,模拟多个窗口(至少4个)同时出售100张车票的情况;用实现Runnable接口的方法实现多线程。
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TicketSell {
public static void main(String[] args) {
Runnable windows = new Ticket();
Thread t1 = new Thread(windows, "--窗口1--");
Thread t2 = new Thread(windows, "--窗口2--");
Thread t3 = new Thread(windows, "--窗口3--");
Thread t4 = new Thread(windows, "--窗口4--");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket implements Runnable{
private final Lock lock = new ReentrantLock(); //线程同步
private int ticketNumber = 100;
@Override
public void run() {
while (ticketNumber > 0){
lock.lock();
try{
if(ticketNumber <= 0) break;
ticketNumber--;
System.out.println(Thread.currentThread().getName()+"出售车票,车票余量:"+ticketNumber);
Thread.sleep(110); //出票需要时间
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
}
}
}
(2)利用多线程机制编写程序,输出一个问候语,要求每隔1秒钟输出一个字符。用2种方法分别创建线程。
例如:问候语=“welcome!”,运行结果可能有多种情况,如下表所示,分别说明原因及创建线程的方法。
第一个:
public class Welcome1 {
public static void main(String[] args) {
Thread t1 = new WelcomeThread();
Thread t2 = new WelcomeThread();
t1.start();
t2.start();
}
}
class WelcomeThread extends Thread{
String welcome = "welcome!";
@Override
public void run() {
for (int i = 0; i < welcome.length(); i++) {
System.out.println(Thread.currentThread().getName()+":"+welcome.charAt(i));
try {
Thread.sleep(1000); //每隔1s输出一个字符
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
第二个:
public class Welcome2 {
public static void main(String[] args) {
String welcome = "welcome!";
Runnable r = () -> {
for (int i = 0; i < welcome.length(); i++){
System.out.println(Thread.currentThread().getName()+":"+welcome.charAt(i));
try {
Thread.sleep(1000); //每隔1s输出一个字符
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};
Thread t = new Thread(r);
t.start();
}
}
第三个:
public class Welcome3 {
public static void main(String[] args) {
Runnable r = new WelcomeRunnable();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
class WelcomeRunnable implements Runnable{
String welcome = "welcome!";
int i = 0;
@Override
public void run() {
while (i < welcome.length() - 1) {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + ":" + welcome.charAt(i));
i++;
try {
Thread.sleep(1000); //每隔1s输出一个字符
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
第四个:
public class Welcome4 {
public static void main(String[] args) {
Runnable r = new WelcomeRunnable1();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}
class WelcomeRunnable1 implements Runnable{
String welcome = "welcome!";
int i = 0;
@Override
public void run() {
while (i < welcome.length()) {
System.out.println(Thread.currentThread().getName() + ":" + welcome.charAt(i));
i++;
try {
Thread.sleep(1000); //每隔1s输出一个字符
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
在此页面也可查看:
https://blog.csdn.net/weixin_57780057/article/details/124518943?spm=1001.2014.3001.5501
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)