服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java多线程模拟实现售票功能

java多线程模拟实现售票功能

2021-12-07 13:41最好的硕硕 Java教程

这篇文章主要为大家详细介绍了java多线程模拟实现售票功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

铁道部发布了一个售票任务,要求销售1000张票,要求有3个窗口来进行销售,请编写多线程程序来模拟这个效果。

1 线程类

测试方法:

?
1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) {
    MyThread t1 = new MyThread("窗口1");
    MyThread t2 = new MyThread("窗口1");
    MyThread t3 = new MyThread("窗口1");
 
    t1.start();
    t2.start();
    t3.start();
 
}

1.1 局部加锁

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MyThread extends Thread{
 
 private static int ticket = 1000;
 private static Object obj = new Object();
 
 public MyThread(String name) {
  super(name);
 }
 
 @Override
 public void run() {
  
  while(ticket > 0){
   synchronized(obj){
    if(ticket > 0){
     System.out.println(Thread.currentThread().getName() + "正在销售第" +(1001-ticket)+ "张票");
     ticket--;
    }
    if(ticket <= 0){
     System.out.println(Thread.currentThread().getName() + "票已售罄");
    }
   }
  }
 }
}

1.2 方法加锁

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class MyThread extends Thread{
 
 private static int ticket = 1000;
 
 public MyThread(String name) {
  super(name);
 }
 
 @Override
 public void run() {
  
  while(ticket > 0){
   method02();
  }
 }
 
 //锁对象:类的字节码文件对象(MyThread.class),有static修饰
 public static synchronized void method02(){
  if(ticket > 0){
   System.out.println(Thread.currentThread().getName() + "正在销售第" +(1001-ticket)+ "张票");
   ticket--;
  }
  if(ticket <= 0){
   System.out.println(Thread.currentThread().getName() + "票已售完");
  }
 }
}

1.3 手动加锁

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MyThread extends Thread{
 
 private static int ticket = 1000;
 private static Lock lock = new ReentrantLock();
 
 public MyThread(String name) {
  super(name);
 }
 
 @Override
 public void run() {
  
  while(ticket > 0){
   lock.lock();//手动上锁
   if(ticket > 0){
    System.out.println(Thread.currentThread().getName() + "正在销售第" +(1001-ticket)+ "张票");
    ticket--;
   }
   if(ticket <= 0){
    System.out.println(Thread.currentThread().getName() + "票已售完");
   }
   lock.unlock();//手动解锁
  }
 }
}

2 任务类

测试方法:

?
1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) {
    Task task = new Task();
    Thread t1 = new Thread(task, "窗口1");
    Thread t2 = new Thread(task, "窗口2");
    Thread t3 = new Thread(task, "窗口3");
    t1.start();
    t2.start();
    t3.start();
 
}

2.1 局部加锁

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Task implements Runnable{
 private int tickets=1000;
 
 @Override
 public void run() {
  while(tickets>0){
   synchronized (this) {
    if(tickets > 0)
             {
                 System.out.printf ("%s窗口正在售出第%d张票\n",Thread.currentThread().getName(),1001-tickets);
                 tickets--;
             }
             if(tickets<=0){
              System.out.printf("%s窗口售罄\n",Thread.currentThread().getName());
             }
   }
   
  }
 }
}

2.2 方法加锁

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Task implements Runnable{
 private int tickets=1000;
 
 @Override
 public void run() {
  while(tickets>0){
   method(); 
  }
 }
    //方法加锁,没有使用static修饰
 public synchronized void method(){
  if(tickets > 0)
        {
            System.out.printf ("%s窗口正在售出第%d张票\n",Thread.currentThread().getName(),1001-tickets);
            tickets--;
        }
        if(tickets<=0){
         System.out.printf("%s窗口售罄\n",Thread.currentThread().getName());
        }
 }
}

2.3 手动加锁

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class Task implements Runnable{
 private int tickets=1000;
 private Lock lock =new  ReentrantLock();
 @Override
 public void run() {
  while(tickets>0){
   lock.lock();//手动上锁
   if(tickets > 0)
            {
                System.out.printf ("%s窗口正在售出第%d张票\n",Thread.currentThread().getName(),1001-tickets);
                tickets--;
            }
            if(tickets<=0){
             System.out.printf("%s窗口售罄\n",Thread.currentThread().getName());
            }
            lock.unlock();//手动关锁
  }
 }
}

效果截图:

java多线程模拟实现售票功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/DengShuo_shuai/article/details/119712285

延伸 · 阅读

精彩推荐