package test; /** * Purpose: 线程和单例模式测试 * @author Hermanwang * @since JDK 1.8 * @date 2018年5月9日 * @MaintenancePersonnel Hermanwang */ public class threadTest{ /** * Purpose:开启线程方法1:实现Runnable接口 * @author Hermanwang * @param args * @return void */ // public static void main(String[] args) { // //实现Runnable接口 // MyThread t = new MyThread(); // new Thread(t).start(); // new Thread(t).start(); // } /** * Purpose: 实现Runnable接口 * @author Hermanwang * @since JDK 1.8 * @date 2018年5月9日 * @MaintenancePersonnel Hermanwang */ class MyThread implements Runnable{ private int sum = 100; @Override public void run() { while (true) if (sum > 0) { System.out.println(Thread.currentThread().getName() + "------" +sum--); } } } /** * Purpose:开启线程方法2:继承Thread类 * @author Hermanwang * @param args * @return void */ // public static void main(String[] args) { // //继承Thread类 // MyThread1 t = new MyThread1(); // MyThread1 t1 = new MyThread1(); // t.start(); // t1.start(); // } /** * Purpose: 继承Thread类 * @author Hermanwang * @since JDK 1.8 * @date 2018年5月9日 * @MaintenancePersonnel Hermanwang */ class MyThread1 extends Thread{ private int sum = 100; @Override public void run() { while (true) synchronized (this) { if (sum > 0) { try { sleep(10); System.out.println(Thread.currentThread().getName() + "------" +sum--); } catch (InterruptedException e) { e.printStackTrace(); } } } } } /** * Purpose:测试死锁 * @author Hermanwang * @param args * @return void */ public static void main(String[] args) { deadLock t = new deadLock(); new Thread(t).start(); //主线程休息五秒,修改 t.flag的值 try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } t.flag = false; new Thread(t).start(); } } /** * Purpose: 测试死锁 * @author Hermanwang * @since JDK 1.8 * @date 2018年5月9日 * @MaintenancePersonnel Hermanwang */ class deadLock implements Runnable{ private int sum = 100; boolean flag = true; Object lockA = new Object(); Object lockB = new Object(); @Override public void run() { if(flag) { while (true) /** * lockA里面调用lockB锁 */ synchronized (lockA) { System.out.println("case--------lockA"); if (sum > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB) { System.out.println("case--------lockB"+sum--); } } } } else { show(); } } /** * lockB里面调用lockA锁 */ private void show() { while (true) synchronized (lockB) { System.out.println("show--------lockB"); if (sum > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockA) { System.out.println("show--------lockA"+sum--); } } } } } /** * Purpose: 单例模式 饿汉式 * @author Hermanwang * @since JDK 1.8 * @date 2018年5月9日 * @MaintenancePersonnel Hermanwang */ class singleton { private static final singleton s = new singleton(); /** * * Constructor:private空构造器 */ private singleton (){ } public static singleton getinstance() { return s; } } /** * Purpose: 单例模式 懒汉式 * @author Hermanwang * @since JDK 1.8 * @date 2018年5月9日 * @MaintenancePersonnel Hermanwang */ class singleton1 { private static singleton1 s = null; /** * * Constructor:private空构造器 */ private singleton1 (){ } public static singleton1 getinstance() { if(s == null) { synchronized (singleton1.class) { if(s == null) { s = new singleton1(); } } } return s; } }
线程和单例模式测试
最新推荐文章于 2022-04-05 17:30:00 发布