线程Thread与Runnable实现

xiaoxiao2021-02-28  107

当new 一个Thread的时候,就是在主线程的基础上再开一个子线程,cpu一会儿给主线程用,一会儿给子线程用,所以多线程会降低工作效率

一:Thread 与Runnable的使用

1:Thread 自己实现自己的run方法

public static void main(String[] args) throws InterruptedException { new Thread() { @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("thread :" + Thread.currentThread().getName()); } } }.start(); }

2:Thread 与Runnabe结合

public static void main(String[] args) throws InterruptedException { new Thread( new Runnable(){ @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("runnable :" + Thread.currentThread().getName()); } } } ).start(); }

二:synchronized的使用

    synchronized能够为代码块,方法加上同步锁,保证数据的一致性,防止在多线程里面出现数据被修改或者输出不一致等情况

1:不加synchronized出现的后果

public static void main(String[] args){ new SynchTest().init(); } private void init(){ final Book outer=new Book(); new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } outer.threadTest("linzeyang"); } } }).start(); new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } outer.threadTest("wangyali"); } } }).start(); } class Book{ public void threadTest(String name){ int len=name.length(); for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } } } 执行这段代码会发现,输出结果中,linzeyang,wangyali,这个完整的字段会被多次打断,出现其他形式的字符串,threadTest方法加上synchronized关键字之后就不会被打断的情况

2:加synchronized出现的结果

public static void main(String[] args){ new SynchTest().init(); } private void init(){ final Book outer=new Book(); new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } outer.threadTest("linzeyang"); } } }).start(); new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } class Book{ public synchronized void threadTest(String name){ int len=name.length(); for(int i=0;i<len;i++){ System.out.print(name.charAt(i)); } System.out.println(); } }

三:wait 和notity的使用

  wait和notity能实现线程之间的通讯,使得现场在同步锁的情况下,可以中断本身的操作,唤醒其他线程的等待状态,变为可执行状态

1:实例应用

子线程循环10次,主线程循环100次,接着子线程再循环十次,主线程再循环100次,如此循环50次

public static void main(String[] args) { final Business business = new Business(); new Thread( new Runnable() { @Override public void run() { //子线程 for(int i=1;i<=50;i++){ business.sub(i); } } } ).start(); //主线程 for(int i=1;i<=50;i++){ business.main(i); } } } class Business { private boolean bShouldSub = true; //子线程循环10次 public synchronized void sub(int i){ while(!bShouldSub){ try { //如果主线程在执行,则等待 this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int j=1;j<=10;j++){ System.out.println("sub thread sequence of " + j + ",loop of " + i); } bShouldSub = false; //执行完毕,唤醒主线程 this.notify(); } //主现场循环100次 public synchronized void main(int i){ while(bShouldSub){ try { //如果子线程在执行,则等待 this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int j=1;j<=100;j++){ System.out.println("main thread sequence of " + j + ",loop of " + i); } bShouldSub = true; //执行完毕,唤醒子线程 this.notify(); }

通过wait和notity来实现主线程与子线程之间的切换

转载请注明原文地址: https://www.6miu.com/read-65171.html

最新回复(0)