实现数据数组冒泡排序
/** * 冒泡实现数组排序 * * @Author J.L.Lee * @Date Aug 18, 2017 */ public class test01 { public static void main(String[] args) { int[] arr = new int[] { 8, 9, 3, 20, 10 }; System.out.println("排序前:" ); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); } for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length – i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp; temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } System.out.println("\n排序后:" ); for (int i = 0; i < arr.length; i++) { System.out.print(+ arr[i] + "\t"); } } }
运行结果:
多线程之售票系统
/** * 12306火车票购票系统 * * 1.模拟系统自动生成3个购票点,实现自动购票; * 2.系统默认测试数据100张车票; * * @Author J.L.Lee * @Date Aug 18, 2017 */ public class Ticket implements Runnable { public int num = 0; Object obj = new Object(); @Override public void run() { while (true) { // 关键字后的小括号表示锁,锁可以是程序中的任意一个对象 synchronized (obj) { if (num >= 100) { //系统默认测试数据100张车票 break; } try { // 当线程睡眠时锁不会开 Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } num++; if(num<10){ System.out.println(Thread.currentThread().getName() + ":购买了第0"+num+"/100张车票" ); }else{ System.out.println(Thread.currentThread().getName() + ":购买了第"+num+"/100张车票" ); } } } } public static void main(String[] args) { Ticket ticket = new Ticket(); //1.创建购票线程 Thread t1 = new Thread(ticket, "售票点1"); Thread t2 = new Thread(ticket, "售票点2"); Thread t3 = new Thread(ticket, "售票点3"); //2.开启购票点(模拟3个线程)测试购票 System.out.println("---------------------------------"); System.out.println("12306火车票购票系统"); System.out.println("---------------------------------"); System.out.println("系统当前在线剩余总票数:100张"); t1.start(); t2.start(); t3.start(); } }
运行结果:
时间: 2024-10-21 04:52:23