package MyThreadDemo; /* 方式1:继承Thread类 定义一个类MyThread继承Thread类 在MyThread类中重写run()方法 创建MyThread类的对象 启动线程 */ public class MyThreadDemo1 { public static void main(String[] args) { MyThread my1 = new MyThread(); MyThread my2 = new MyThread(); // my1.run(); // my2.run(); // void start() 导致此线程开始执行; Java虚拟机调用此线程的run方法。 my1.start(); my2.start(); } }
两个小问题:
MyThread类
package MyThreadDemo; public class MyThread1 extends Thread{ public MyThread1(){} public MyThread1(String name){ super(name); } @Override public void run() { for (int i = 0; i <100 ; i++) { System.out.println(getName()+":"+i); } } } /* public Thread() { init(null, null, "Thread-" + nextThreadNum(), 0); } */
执行类
package MyThreadDemo; public class MyThreadDemo2 { public static void main(String[] args) { // MyThread1 th1 = new MyThread1(); // MyThread1 th2 = new MyThread1(); // // th1.setName("高铁"); // th2.setName("飞机"); //Thread(String name) 分配一个新的 Thread对象。 // MyThread1 th1 = new MyThread1("高铁"); // MyThread1 th2 = new MyThread1("飞机"); // // th1.start(); // th2.start(); //static Thread currentThread() 返回对当前正在执行的线程对象的引用。 System.out.println(Thread.currentThread().getName()); } }
package MyThreadDemo; public class MyThreadDemo3 { public static void main(String[] args) { ThreadPriority th1 = new ThreadPriority(); ThreadPriority th2 = new ThreadPriority(); ThreadPriority th3 = new ThreadPriority(); th1.setName("高铁"); th2.setName("飞机"); th3.setName("汽车"); //public final int getPriority():返回此线程的优先级 // System.out.println(th1.getPriority()); // System.out.println(th2.getPriority()); // System.out.println(th3.getPriority()); //public final void setPriority(int newPriority):更改此线程的优先级 // th1.setPriority(10000); // System.out.println(Thread.MAX_PRIORITY); // System.out.println(Thread.MIN_PRIORITY); // System.out.println(Thread.NORM_PRIORITY); //设置正确优先级 th1.setPriority(5); th2.setPriority(10); th3.setPriority(1); th1.start(); th2.start(); th3.start(); } }
ThreadSleep类
package MyThreadDemot; public class ThreadSleep extends Thread{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(getName()+","+i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
测试代码
package MyThreadDemot; /* static void sleep(Long millis),使当前正在执行的线程停留(暂停执行)指定的毫秒数 */ public class ThreadSleepDemo { public static void main(String[] args) { ThreadSleep s1 = new ThreadSleep(); ThreadSleep s2 = new ThreadSleep(); ThreadSleep s3 = new ThreadSleep(); s1.setName("黄忠"); s2.setName("赵云"); s3.setName("马超"); s1.start(); s2.start(); s3.start(); } }
ThreadJoin类
package MyThreadDemot; public class ThreadJoin extends Thread{ @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(getName() + "," + i); } } }
测试代码
package MyThreadDemot; public class ThreadJoinDemo { public static void main(String[] args) { ThreadJoin s1 = new ThreadJoin(); ThreadJoin s2 = new ThreadJoin(); ThreadJoin s3 = new ThreadJoin(); s1.setName("钟"); s2.setName("涛"); s3.setName("猛"); s1.start(); try { s1.join(); } catch (InterruptedException e) { e.printStackTrace(); } s2.start(); s3.start(); } }
ThreadDaemon类
package MyThreadDemot; public class ThreadDaemon extends Thread { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(getName()+","+i); } } }
测试代码
package MyThreadDemot; // void setDaemon(boolean on) 将此线程标记为守护线程当运行的线程都是守护线程时,虚拟机将退出 public class ThreadDaemonDemo { public static void main(String[] args) { ThreadDaemon d1 = new ThreadDaemon(); ThreadDaemon d2 = new ThreadDaemon(); d1.setName("关羽"); d2.setName("张飞"); //设置主线程 Thread.currentThread().setName("刘备"); //设置守护线程 d1.setDaemon(true); d2.setDaemon(true); d1.start(); d2.start(); for (int i = 0; i <10 ; i++) { System.out.println(Thread.currentThread().getName()+","+i); } } }
package MyThreadDemo; public class MyRunnableDemo1 { public static void main(String[] args) { //创建MyRunnbale类的对象 MyRunnable my = new MyRunnable(); //创建Thread类对象,把MyRunnable对象作为构造方法的参数 // Thread(Runnable target) 分配一个新的 Thread对象。 // Thread t1 = new Thread(my); // Thread t2 = new Thread(my); // Thread(Runnable target, String name) 分配一个新的 Thread对象。 Thread t1 = new Thread(my,"高铁"); Thread t2 = new Thread(my,"飞机"); //启动线程 t1.start(); t2.start(); } }
SellTicker类
package MyThreadDemo; public class SellTicker implements Runnable { private int tickers = 100; @Override public void run() { while (true) { if (tickers > 0) { System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); tickers--; } } } }
SellTickerDemo测试类
package MyThreadDemo; public class MyThreadDemo4 { public static void main(String[] args) { SellTicker st = new SellTicker(); Thread t1 = new Thread(st, "窗口1"); Thread t2 = new Thread(st, "窗口2"); Thread t3 = new Thread(st, "窗口3"); t1.start(); t2.start(); t3.start(); } }
package MyThreadDemo; public class SellTicker implements Runnable { private int tickers = 100; @Override public void run() { //相同的票出现了多次 // while (true) { // //tickers = 100; // //t1,t2,t3 // //假设t1的线程抢到CPU的执行权 // if (tickers > 0) { // //通过sleep()方法来模拟出票时间 // try { // Thread.sleep(100); // //t1线程休息100毫秒 // //t2线程检测了CPU的执行权,t2线程就开始执行,执行到这里的时候,t2线程也休息100毫秒 // //t3线程检测了CPU的执行权,t2线程就开始执行,执行到这里的时候,t3线程也休息100毫秒 // } catch (InterruptedException e) { // e.printStackTrace(); // } // //假设线程按照顺序醒过来 // //t1抢到CPU的执行权,在控制台输出,窗口1正在出售第100张票 // System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); // //t2抢到了CPU的执行权,在控制台输出,窗口2正在出售第100张票 // //t3抢到了CPU的执行权,在控制台输出,窗口3正在出售第100张票 // tickers--; // // //如果这三个线程还是按照顺序来,这里就执行了3次--操作,最终票就变成了97 // } // } //出现了负数的票 while (true) { //tickers = 100; //t1,t2,t3 //假设t1的线程抢到CPU的执行权 if (tickers > 0) { //通过sleep()方法来模拟出票时间 try { Thread.sleep(100); //t1线程休息100毫秒 //t2线程检测了CPU的执行权,t2线程就开始执行,执行到这里的时候,t2线程也休息100毫秒 //t3线程检测了CPU的执行权,t2线程就开始执行,执行到这里的时候,t3线程也休息100毫秒 } catch (InterruptedException e) { e.printStackTrace(); } //假设线程按照顺序醒过来 //t1 抢到了CPU的执行权,在控制台输出,窗口1正在出售第1张票 //假设t1继续拥有CPU的执行权,就会执行tickers--,操作,tickers = 0 //t1 抢到了CPU的执行权,在控制台输出,窗口1正在出售第0张票 //假设t2继续拥有CPU的执行权,就会执行tickers--,操作,tickers = -1 //t3 抢到了CPU的执行权,在控制台输出,窗口1正在出售第-1张票 //假设t3继续拥有CPU的执行权,就会执行tickers--,操作,tickers = -2 System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); tickers--; } } } }
MyTheread测试代码
package MyThreadDemo; /* 需求:某电影院目前正在上映国产大片,共有100张票,而它有3个窗口卖票,请设计一个程序模拟该电影院卖票 思路: 定义一个类SellTicker实现Runnbale接口,里面定义一个成员变量:private int tickets = 100; 在SellTicker类中重写run()方法实现卖票,代码步骤如下: A:判断票数大于0,就卖票,并告知是哪个窗口卖的 B:卖了票之后总票数减1; C:票没有了,也可能有人来问,所以这里用死循环让卖票的动作一直执行 定义一个测试类SellTickerDemo,里面有main方法,代码步骤如下 A:创建SellTicker类的对象 B:创建三个Thread类的对象,把SellTIcker对象作为构造方法的参数,并给出对应的窗口名称 C:启动线程 */ public class MyThreadDemo4 { public static void main(String[] args) { SellTicker st = new SellTicker(); Thread t1 = new Thread(st, "窗口1"); Thread t2 = new Thread(st, "窗口2"); Thread t3 = new Thread(st, "窗口3"); t1.start(); t2.start(); t3.start(); } }
卖票案例数据安全问题的解决
同步代码块
synchronized(任意对象){
多条语句操作共享数据的代码
}
SellTicker类
package MyThreadDemo; public class SellTicker1 implements Runnable{ private int tickers = 100; private Object obj = new Object(); @Override public void run() { while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (obj) { if (tickers > 0) { System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); tickers--; } } } } }
测试代码
package MyThreadDemo; public class MyThreadDemo5 { public static void main(String[] args) { SellTicker1 st = new SellTicker1(); Thread t1 = new Thread(st, "窗口1"); Thread t2 = new Thread(st, "窗口2"); Thread t3 = new Thread(st, "窗口3"); t1.start(); t2.start(); t3.start(); } }
同步的好处和弊端
同步方法
package MyThreadDemo; public class SellTicker2 implements Runnable { // private int tickers = 100; private static int tickers = 100; private Object obj = new Object(); private int x = 0; @Override public void run() { while (true) { if (x % 2 == 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } // synchronized (obj) // synchronized (this){ synchronized (SellTicker.class){ if (tickers > 0) { System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); tickers--; } } }else { // try { // Thread.sleep(100); // } catch (InterruptedException e) { // e.printStackTrace(); // } // synchronized (obj) { // if (tickers > 0) { // System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); // tickers--; // } // } sellTicker(); } x++; } } // private void sellTicker() { // try { // Thread.sleep(100); // } catch (InterruptedException e) { // e.printStackTrace(); // } // synchronized (obj) { // if (tickers > 0) { // System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); // tickers--; // } // } // } //private synchronized void sellTicker() { // try { // Thread.sleep(100); // } catch (InterruptedException e) { // e.printStackTrace(); // } // if (tickers > 0) { // System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); // tickers--; // } // } private static synchronized void sellTicker() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (tickers > 0) { System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); tickers--; } } } /* 这个程序是一对一下进行执行的,当sellTicker不为静态方法时,使用synchronized,对应的锁为this, 当sellTicker为静态方法时,使用synchronized对应的锁为SellTicker.class. */
线程安全的类
Lock锁
package MyThreadDemo; /* void lock():获得锁 void unlock(): 释放锁 */ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class SellTrick3 implements Runnable{ private int tickers = 100; private Lock lock = new ReentrantLock(); @Override public void run() { while (true) { lock.lock(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (tickers > 0) { System.out.println(Thread.currentThread().getName() + "正在出售票" + tickers + "张票"); tickers--; } lock.unlock(); } } }
原文:https://www.cnblogs.com/Xinyi-bigdata/p/15158231.html