1.并发:万物都可以同时完成很多工作,我们把这些活动同时进行,这种同时进行的思想在Java中成为并发。
2.线程:将并发完成的每一件事情我们成为线程。
3.实现线程的2种方式:
4.继承Thread类:
package xiancheng; /** * 创建线程 */ public class ThreaTest extends Thread { private int count = 10; //覆写run()方法,完成线程真正功能 public void run(){ System.out.println("创建了一个新线程ThreaTest,来实现业务!"); while (true){ System.out.print(count+" "); if(--count == 0){ return; } } } public static void main(String[] args) { //线程的启动是调用start()方法 ThreaTest threaTest = new ThreaTest(); threaTest.start(); } } 运行结果: 创建了一个新线程ThreaTest,来实现业务! 10 9 8 7 6 5 4 3 2 1
5.实现Runnable 接口:
package xiancheng; public class RunnableTest { private static Thread thread; private int count = 10; public void getInfo(){ thread = new Thread(new Runnable() { @Override public void run() { while (true){ System.out.print(count+" "); if(--count == 0){ return; } } } }); thread.start(); } public static void main(String[] args) { RunnableTest runnableTest = new RunnableTest(); runnableTest.getInfo(); } } 运行结果: 10 9 8 7 6 5 4 3 2 1
6.线程的生命周期的七种状态:
7.线程的加入:join()方法
package xiancheng; import static java.lang.Thread.*; public class JoinTest { private static Thread threadA; private static Thread threadB; public JoinTest(){ threadA = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(10000); threadB.join(); System.out.println("线程threadA执行完毕"); } catch (InterruptedException e) { e.printStackTrace(); } } }); threadA.start(); threadB = new Thread(new Runnable() { @Override public void run() { System.out.println("线程threadB执行完毕"); } }); threadB.start(); } public static void main(String[] args) { JoinTest joinTest = new JoinTest(); } } 运行结果: 线程threadB执行完毕 线程threadA执行完毕
7.线程中断 interrupt():
package xiancheng; public class InterruptedTest { private static Thread threadA; public InterruptedTest() { threadA = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(10000); System.out.println("线程threadA执行中"); } catch (InterruptedException e) { System.out.println("线程被中断"); } } }); threadA.start(); threadA.interrupt(); } public static void main(String[] args) { InterruptedTest joinTest = new InterruptedTest(); } }
8:线程的礼让,使用yield()方法。
9:线程的优先级:Thread.MIN_PRIORITY(常量1),Thread.MAX_PRIORITY(常量10),Thread.NORM_PRIORITY(常量5),常量越大优先级越高。
10:线程同步:Java中提供线程同步的机制来防止访问资源的冲突。使用同步锁解决:
package xiancheng; public class ThreadSafeTest implements Runnable { int num = 10; @Override public void run() { while(true){ synchronized (""){ if(num >0 ){ try { Thread.sleep(000); }catch (Exception e){ e.printStackTrace(); } System.out.println("tickets"+(--num)); } } } } public static void main(String[] args) { ThreadSafeTest threadSafeTest = new ThreadSafeTest(); Thread threadA = new Thread(threadSafeTest); Thread threadB = new Thread(threadSafeTest); Thread threadC = new Thread(threadSafeTest); Thread threadD = new Thread(threadSafeTest); threadA.start(); threadB.start(); threadC.start(); threadD.start(); } } 运行结果: tickets9 tickets8 tickets7 tickets6 tickets5 tickets4 tickets3 tickets2 tickets1 tickets0
原文:https://www.cnblogs.com/zwh820672664/p/10836025.html