您现在的位置是:主页 > news > 音乐网站的制作/h5下一页
音乐网站的制作/h5下一页
admin2025/5/26 0:47:53【news】
简介音乐网站的制作,h5下一页,网站建设适合什么单位,聚美优品卖票 package sq.com.exer;/*** 例子:创建三个窗口卖票,总票数 100 张,使用实现 Runnable 接口的方式* 存在线程的安全问题,待解决**/ class Window extends Thread{private static int ticket 100;Overridepublic void run() {…
音乐网站的制作,h5下一页,网站建设适合什么单位,聚美优品卖票
package sq.com.exer;/*** 例子:创建三个窗口卖票,总票数 100 张,使用实现 Runnable 接口的方式* 存在线程的安全问题,待解决**/
class Window extends Thread{private static int ticket 100;Overridepublic void run() {…
卖票
package sq.com.exer;/*** 例子:创建三个窗口卖票,总票数 100 张,使用实现 Runnable 接口的方式* 存在线程的安全问题,待解决**/
class Window extends Thread{private static int ticket = 100;@Overridepublic void run() {while(true){if(ticket > 0){System.out.println(getName()+": 卖票,票号为:" + ticket);ticket--;} else{break;}}}
}
public class WindowTest {public static void main(String[] args) {Window t1 = new Window();Window t2 = new Window();Window t3 = new Window();t1.setName("窗口1");t2.setName("窗口2");t3.setName("窗口3");t1.start();t2.start();t3.start();}
}
package sq.com.exer;
/*** 例子:创建三个窗口卖票,总票数 100 张,使用实现 Runnable 接口的方式* 存在线程的安全问题,待解决**/
class Window1 implements Runnable{// 注意 这里没有加 staticprivate int ticket = 100;@Overridepublic void run() {while(true){if(ticket > 0){System.out.println(Thread.currentThread().getName()+": 卖票,票号为: " + ticket);ticket--;} else{break;}}}
}
public class WindowTest1 {public static void main(String[] args) {Window1 w = new Window1();// 传递的是同一个对象,所以变量没有加 staticThread t1 = new Thread(w);Thread t2 = new Thread(w);Thread t3 = new Thread(w);t1.setName("窗口1");t2.setName("窗口2");t3.setName("窗口3");t1.start();t2.start();t3.start();}
}
package com.sq.java;
/*** 例子:创建三个窗口卖票,总票数 100 张,使用实现 Runnable 接口的方式** 1.问题:卖票过程中,出现了重票、错票 --> 出现了线程的安全问题* 2.问题出现的原因:当某个线程操作车票的过程中,尚未操作完成时,其他线程参与进来,也操作车票* 3.如何解决:当一个线程 a 在操作 ticket 的时候,其他线程不能参与进来。直到线程 a 操作完 ticket时,其他* 线程才可以开始操作 ticket。这种情况即使线程 a 出现了阻塞,也不能被改版。* 4.在 Java 中,我们通过同步机制,来解决线程的安全问题** 方式一:同步代码块* synchronized(同步监视器){* // 需要被同步的代码** }* 说明: 1.操作共享数据的代码,即为需要被同步的代码。--> 不能包含代码多了,也不能包含代码少了。* 2.共享数据:多个线程共同操作的变量。比如:ticket 就是共享数据* 3.同步监视器,俗称:锁。任何一个类的对象,都可以充当锁* 要求:多个线程必须要共用同一把锁** 补充:在实现 Runnable 接口创建多线程的方式中,我们可以考虑使用 this 充当同步监视器。** 方式二:同步方法* 如果操作共享数据的代码完整的声明在一个方法中,我们不妨将此方法声明同步的。** 5.同步的方式,解决了线程的安全问题。--> 好处* 操作同步代码时,只能有一个线程参与,其他线程等待,相当于是一个单线程的过程,效率低。 --> 局限性***/
class Window1 implements Runnable{private static int ticket = 100;
// Object obj = new Object();
// Dog dog = new Dog();private static Object obj = new Object();@Overridepublic void run() {// 放在这里就不是公用一把锁了
// Object obj = new Object();while(true){synchronized (obj){if(ticket > 0){try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+": 卖票,票号为:" + ticket);ticket--;} else{break;}}}}
}public class WindowTest1 {public static void main(String[] args) {Window1 w = new Window1();// 传递的是同一个对象,所以变量没有加 staticThread t1 = new Thread(w);Thread t2 = new Thread(w);Thread t3 = new Thread(w);t1.setName("窗口1");t2.setName("窗口2");t3.setName("窗口3");t1.start();t2.start();t3.start();}
}
//class Dog{}
package com.exer;
/*** 使用同步代码块解决继承 Thread 类的方式的线程安全问题** 例子:创建三个窗口卖票,总票数 100 张,使用实现 Runnable 接口的方式** 说明:在继承 Thread 类创建多线程的方式中,慎用 this 充当同步监视器,考虑使用当前类充当充当同步监视器。**/
class Window2 extends Thread{private static int ticket = 100;private static Object obj = new Object();@Overridepublic void run() {while(true){// 正确的
// synchronized(obj){synchronized(Window2.class){ // Class clazz = Window2.class Window2.class只会加载一次// 错误的方式:this代表着 t1,t2,t3
// synchronized(this){if(ticket > 0){try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+": 卖票,票号为: " + ticket);ticket--;}}}}
}public class WindowTest2 {public static void main(String[] args) {Window2 t1 = new Window2();Window2 t2 = new Window2();Window2 t3 = new Window2();t1.setName("窗口1");t2.setName("窗口2");t3.setName("窗口3");t1.start();t2.start();t3.start();}
}
package com.exer;/*** 使用同步方法解决实现 Runnable 接口的线程安全问题* ctrl + alt + l 自动对齐代码*/
class Window3 implements Runnable {private static int ticket = 100;@Overridepublic void run() {while (true) {show();}}private synchronized void show(){ // 同步监视器:this
// synchronized (this){if (ticket > 0) {try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + ": 卖票,票号为:" + ticket);ticket--;}
// }}
}public class WindowTest3 {public static void main(String[] args) {Window3 w = new Window3();// 传递的是同一个对象,所以变量没有加 staticThread t1 = new Thread(w);Thread t2 = new Thread(w);Thread t3 = new Thread(w);t1.setName("窗口1");t2.setName("窗口2");t3.setName("窗口3");t1.start();t2.start();t3.start();}
}
package com.exer;/*** 使用同步方法处理继承 Thread 类的方式中的线程安全问题** 关于同步方法的总结:* 1.同步方法仍然涉及到同步监视器,只是不需要我们显示的声明。* 2.非静态的同步方法,同步监视器是:this* 静态的同步方法,同步监视器是:当前类本身*/
class Window4 extends Thread {private static int ticket = 100;@Overridepublic void run() {while (true) {show();}}private static synchronized void show(){ // 同步监视器:(静态里不能调this) Window4.class// private synchronized void show(){ // 同步监视器:t1,t2,t3。此种解决方式是错误的if (ticket > 0) {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + ": 卖票,票号为: " + ticket);ticket--;}}
}public class WindowTest4 {public static void main(String[] args) {Window4 t1 = new Window4();Window4 t2 = new Window4();Window4 t3 = new Window4();t1.setName("窗口1");t2.setName("窗口2");t3.setName("窗口3");t1.start();t2.start();t3.start();}
}
package com.exer;/*** 使用同步机制将单例模式中的懒汉式改写为线程安全的**/
public class BankTest {}
// 单例模式:懒汉式
class Bank{private Bank(){}private static Bank instance = null;// 解决了线程不安全
// public static synchronized Bank getInstance(){public synchronized Bank getInstance(){// 方法一:效率稍差
// synchronized (Bank.class) {
// if(instance == null){
// instance = new Bank();
// }
// }
// return instance;// 方式二:效率更高if(instance == null){synchronized (Bank.class) {if(instance == null){instance = new Bank();}}}return instance;}
}