> 文档中心 > JAVA实验8 多线程编程

JAVA实验8 多线程编程


实验8 多线程编程

实验要求:
(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();     } }    }}

JAVA实验8 多线程编程

(2)利用多线程机制编写程序,输出一个问候语,要求每隔1秒钟输出一个字符。用2种方法分别创建线程。
例如:问候语=“welcome!”,运行结果可能有多种情况,如下表所示,分别说明原因及创建线程的方法。
JAVA实验8 多线程编程
第一个:

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);     } }    }}

JAVA实验8 多线程编程

第二个:

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();    }}

JAVA实验8 多线程编程

第三个:

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);  }     } }    }}

JAVA实验8 多线程编程

第四个:

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);     } }    }}

JAVA实验8 多线程编程

麦克风网