多线程
线程的概述
- 每个运行的程序都是一个进程,在一个进程中还可以有多个执行单元同时运行,这些执行单元可以看做程序执行的一条条线索,被称为线程。操作系统中的每一个进程中都至少存在一个线程。
线程的创建
创建线程有两种方法,一是继承java.Lang包下的Thread类,覆写Thread类的run()方法,在run方法()中实现运行在线程上的代码;二是实现java.lang.Runnable接口,在run方法()中实现运行在线程上的代码。
单线程:
package day_Thread; public class Example01 extends Thread{ public static void main(String[] args) { MyThread mythread=new MyThread(); mythread.run(); while(true){ System.out.println("Main方法在运行"); } } } class MyThread{ public void run(){ while(true){ System.out.println("Mythread类的run()方法在运行"); } } }
继承Thread类创建多线程:
package day_Thread; public class Example02 extends Thread{ public static void main(String[] args) { //创建线程Mythread的线程对象; MyThread1 mythread=new MyThread1(); //开启线程 mythread.start(); //通过死循环语句打印输出 while(true){ System.out.println("Main方法在运行"); } } } class MyThread1 extends Thread{ public void run(){ while(true){ System.out.println("Mythread类的run()方法在运行"); } } }
实现Runable接口创建多线程
package day_Thread; public class Example02 extends Thread{ public static void main(String[] args) { //创建线程Mythread的线程对象; MyThread1 mythread=new MyThread1(); //开启线程 mythread.start(); //通过死循环语句打印输出 while(true){ System.out.println("Main方法在运行"); } } } class MyThread1 extends Thread{ public void run(){ while(true){ System.out.println("Mythread类的run()方法在运行"); } } }
线程的优先级:
- 在应用程序中,如果对线程进行调度,最直接的方法就是设置线程的优先级。
- static int MAX_PRIORITY:表示线程的最高优先级,相当于10;
- static int MIN_PRIORITY:表示线程的最低优先级,相当于1;
static int NORM_PRIORITY:表示线程的普通优先级,相当于5;
package day_Thread; public class Example04 implements Runnable { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + "正在输出:" + i); } } } class MinProiority implements Runnable { @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+"正在输出"+i); } } } //测试类 package day_Thread; public class Test04 { public static void main(String[] args) { Thread minPriority=new Thread(new MinProiority(),"优先级较低的线程"); Thread maxPriority=new Thread(new MinProiority(),"优先级较高的线程"); minPriority.setPriority(Thread.MIN_PRIORITY); maxPriority.setPriority(10); //开启两个线程 maxPriority.start(); minPriority.start(); } }
线程休眠
当线调用sleep(long millis)方法后,可以让当前正在执行的线程暂停一段时间暂停一段时间,进入休眠状态。
package day_Thread; public class Example05 implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for(int i=1;i<=10;i++){ if(i==3){ try{ //当线程休眠2秒 Thread.sleep(2000); } catch(InterruptedException e){ e.printStackTrace(); } } System.out.println("线程一正在输出:"+i); try{ //当前线程休眠500毫秒 Thread.sleep(500); }catch(Exception e){ e.printStackTrace(); } } } } //测试类 package day_Thread; public class Test05 { public static void main(String[] args) throws Exception { new Thread(new Example05()).start(); for(int i=0;i<10;i++){ if(i==5){ Thread.sleep(2000); } System.out.println("主线程正在输出:"+i); Thread.sleep(500); } } }
多线程同步(限制某个资源在同一时刻只能被一个线程访问)
线程安全
package day_Thread; public class Example06 implements Runnable{ private int tickets=10; @Override public void run() { // TODO Auto-generated method stub while(tickets>0){ try { Thread.sleep(10); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"----卖出的票"+tickets--); } } } //测试类 package day_Thread; public class Test06 { public static void main(String[] args) { //创建example06对象 Example06 example06 =new Example06(); //创建并开启四个线程 new Thread(example06,"线程1").start(); new Thread(example06,"线程2").start(); new Thread(example06,"线程3").start(); new Thread(example06,"线程4").start(); } }
同步代码块(使用synchronized关键字来修饰)
package day_Thread; public class Example07 implements Runnable{ private int tickets=10; Object lock=new Object(); @Override public void run() { // TODO Auto-generated method stub while(true){ //操作共享资源代码块 synchronized (lock) { try { Thread.sleep(10); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } if(tickets>0){ System.out.println(Thread.currentThread().getName()+"----卖出的票"+tickets--); }else{ break; } } } } } //测试类 package day_Thread; public class Test07 { public static void main(String[] args) { Example07 example07=new Example07(); //创建并开启四个线程 new Thread(example07,"线程1").start(); new Thread(example07,"线程2").start(); new Thread(example07,"线程3").start(); new Thread(example07,"线程4").start(); } }