本篇讲解的是线程的一些基本使用与讲解,如果你是经常使用线程操作的话,可以对下面内容快速扫一遍即可,因为本篇是线程系列的第一篇,可能后面几篇对某些人来说有所帮助
(一)前介
1)前言:操作系统支持多个任务,一个任务通常就是一个程序,一个运行的程序就是一个进程(在系统中是独立存在的实体)。 进程与线程的关系就如操作系统与进程的关系一样。一个进程可以拥有多个线程。
2)建议实现Callable接口和Runnable接口实现多线程。
3)线程自带的方法解析:
1. join()方法:让一个线程等待另一个线程完成的方法。比如线程A与B,如果线程B调用了join()方法,
则线程A必须等线程B执行完任务之后,线程A才能执行。
2. setDaemon()方法:将普通线程变为后台线程,如果前台线程全部死亡,则后台线程自动死亡。
比如JAM的垃圾回收线程就是典型的后台线程。
3. sleep()方法:暂停线程,让线程处于阻塞状态。
4. yield()方法:暂停线程,不会阻塞线程,让线程处于就绪状态。即让线程调度器又将其调度出来重新执行。
5. setPriority()方法:设置线程的优先级。【注意:设置最高线程的优先级不一定最先执行,只能保证在大部分情况下最先执行】
4)线程同步synchronized:如售卖火车牌,取钱等等案列。
[刚好与异步相反]:线程同步的同步保证了线程安全,数据安全,但却降低了代码的运行速度。
5)同步锁lock:是synchronized的加强版。
使用模板:
ReentrantLock lock = new ReentrantLock();
lock.lock(); //加锁
try {
//需要保证线程安全的代码
} catch (Exception e) {
e.printStackTrace();
}finally{
lock.unlock();
}
(二)前介中的实现demo
1)线程的并发机制
//定义Runnable public class MyRunnable implements Runnable{ private String threadName; public MyRunnable(String threadName){ this.threadName = threadName; } @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println(threadName+i); } } }
public static void main(String[] args) { /** * 线程A与线程B测试实现线程的并发机制(两种方式) * * (两个线程谁先抢到CPU资源谁就执行) */ /*MyThread myThread = new MyThread("A"); MyThread myThread2 = new MyThread("B"); myThread.start(); myThread2.start();*/ MyRunnable myRunnable = new MyRunnable("A"); MyRunnable myRunnable2 = new MyRunnable("B"); Thread thread = new Thread(myRunnable); Thread thread2 = new Thread(myRunnable2); thread.start(); thread2.start(); }
2)线程的优先级的demo
//定义Runnable class myRun implements Runnable{ @Override public void run() { for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); //线程睡眠1秒钟 //打印那个线程执行的多一些 System.out.println(Thread.currentThread().getName()+":"+i); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public static void main(String[] args) { Thread thread1 = new Thread(new myRun(),"A"); Thread thread2 = new Thread(new myRun(),"B"); Thread thread3 = new Thread(new myRun(),"C"); thread1.setPriority(Thread.MIN_PRIORITY);//设置线程A的优先级为最小 thread2.setPriority(Thread.NORM_PRIORITY);//设置线程A的优先级为最默认5 thread3.setPriority(Thread.MAX_PRIORITY);//设置线程C的优先级为最大 thread1.start(); thread2.start(); thread3.start();
//(大部分情况下)结果线程C最先执行完,因为线程C的优先级设置为了最大,所以抢占CPU资源的几率就最大,
也就比A,B线程的更先执行,而A线程最后执行完,因为其优先级设置的最小
}
(三)线程的同步锁demo [测试时,可以先去掉同步锁试一试代码的运行情况]
//定义Runnable class MyRunn implements Runnable{ /** * 公交车售票,默认还剩下8张票,有三个窗口在售票(有三个窗口:就是三个线程) * (只有进行同步操作,才能进行资源共享) */ private int tickets = 8; @Override public void run() { for (int i = 0; i < 10; i++) { tell(); } System.out.println("车票已售完"); } public synchronized void tell(){ if (tickets>0) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("车票:"+tickets--); } } }
public static void main(String[] args) { /** * 解决多线程实现 多线程资源共享 */ MyRunn myRunn = new MyRunn(); Thread thread1 = new Thread(myRunn); Thread thread2 = new Thread(myRunn); Thread thread3 = new Thread(myRunn); thread1.start(); thread2.start(); thread3.start(); }
(四)线程的礼让
class MyRunnables implements Runnable{ private String threadName; public MyRunnables(String threadName){ this.threadName = threadName; } @Override public void run() { for (int i = 0; i < 50; i++) { System.out.println(threadName+i); if (i == 10) { System.out.println("礼让"); Thread.yield(); } } }
}
public static void main(String[] args) { MyRunnables MyRunnables1 = new MyRunnables("A"); MyRunnables MyRunnables2 = new MyRunnables("B"); Thread myThread = new Thread(MyRunnables1); Thread myThread2 = new Thread(MyRunnables2); myThread.start(); myThread2.start(); }至此线程的基本常用操作已经ok