这篇文章主要讲解了“怎么用java多线程模拟实现售票功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用java多线程模拟实现售票功能”吧!
1 线程类
测试方法:
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 局部加锁
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 方法加锁
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 手动加锁
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 任务类
测试方法:
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 局部加锁
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 方法加锁
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 手动加锁
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多线程模拟实现售票功能”的内容了,经过本文的学习后,相信大家对怎么用java多线程模拟实现售票功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。