黑马程序员——多线程

来源:互联网 发布:fresh 红茶面膜 知乎 编辑:程序博客网 时间:2024/06/09 20:35

------- android培训、java培训、期待与您交流! ----------


        每个正在系统上运行的程序都是一个进程。每个进程包含一到多个线程。进程也可能是整个程序或者是部分程序的动态执行。线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行。也可以把它理解为代码运行的上下文。所以线程基本上是轻量级的进程,它负责在单个程序里执行多任务。通常由操作系统负责多个线程的调度和执行。
        线程是程序中一个单一的顺序控制流程.在单个程序中同时运行多个线程完成不同的工作,称为多线程.
       利用多线程技术可以节约CPU时间,提高程序执行效率。
多线程的实现方式有两种:

继承Thread类复写其run()方法实现多线程

public class Test extends Thread {public void run() { //要多线程执行的代码}}
//调用时直接new该对象调用start()方法实现多线程;

public static void main(String[] args) {new Mythread().start();}

实现Runnable接口复写run()方法实现多线程

class Mythread implements Runnable{//实现Runna接口复写run()方法实现多线程public void run() {//多线程执行的代码}}
//调用时将对象传入Thread()方法实现多线程
public static void main(String[] args) {new Thread(new Mythread()).start();}
实现多线程一般都是实现Runnab接口的方式。原因是因为在Java中只允许单继承,而实现接口的方法就不存在这种缺陷,显得更为灵活。

一个简单的多线程程序:

public class hello extends Thread {private String name;public hello() {}public hello(String name) {this.name = name;}public void run() {for (int i = 0; i < 5; i++) {System.out.println(name + "运行     " + i);}}public static void main(String[] args) {hello h1 = new hello("线程A");Thread demo = new Thread(h1);hello h2 = new hello("线程B");Thread demo1 = new Thread(h2);demo.start();demo1.start();}}

synchronized关键字

当两个或者两个以上的线程在同时访问同一资源时,就会出现线程数据错误的问题;要解决这个问题就要使用同步

public class Test22 implements Runnable {private static int num = 20;public void run() {this.show();}private synchronized void show() {do {// synchronized (mutex) {  也可以在此用同步代码块实现if (num > 0) {System.err.println(Thread.currentThread().getName()+ "------------|" + num);num--;}// }} while (true);}}
public static void main(String[] args) {Test22 test22 = new Test22();new Thread(test22).start();new Thread(test22).start();new Thread(test22).start();new Thread(test22).start();}


当某一个线程执行到show()方法时,show()方法就会形成一个锁,只用等该线程把该方法程执行完该函数是这个锁才会解开,这时其他线程才有机会获得cpu的执行权.

注:1、当修饰符放在方法上:该方法同步执行,同步锁是this,但是该方法是静态的时候,同步锁是该类所对应的Class对象。

         2、单独写成同步代码块:实现方式如synchronize(同步锁 ){同步执行的代码}

在JDK5.0以后,Lock 替代了 synchronized 方法和语句的使用,Condition 替代了 Object 监视器方法的使用。

一个生产者和消费者的示例:

Res类:

public class Res {private String name;private int count = 1;private boolean flag = false;private Lock lock = new ReentrantLock(); //新建一个Lock 替代了 synchronizedprivate Condition condition_set = lock.newCondition(); //新建一个Condition 替代了 Object 监视器方法的使用private Condition condition_out = lock.newCondition(); //新建一个Condition 替代了 Object 监视器方法的使用public void set(String name) {lock.lock();  //获取锁try {while(flag) condition_set.await(); //线程等待 会抛出异常this.name = name + "-----------" + count++;System.out.println(Thread.currentThread().getName() + "|生产者|"+ this.name);flag = true;condition_out.signal();  //唤醒等待的消费者线程} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock(); //finall代码必须执行,所以在这里释放锁}}public void out() {lock.lock();try {while(!flag) condition_out.await();  System.out.println(Thread.currentThread().getName() + "|消费者|"+ this.name);flag = false; condition_set.signalAll();  //唤醒等待的消费者线程} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}}}
生产者类:
public class Shengcan implements Runnable {private Res res;public Shengcan(Res res) {this.res = res;}public void run() {while (true) {res.set("商品");}}}
消费者类:

public class Xiaofei implements Runnable {private Res res;public Xiaofei(Res res) {this.res = res;}public void run() {while (true) {res.out();}}}
主函数的调用:
public class Test {public static void main(String[] args) {Res res = new Res();Shengcan shengcan = new Shengcan(res);Xiaofei xiaofei = new Xiaofei(res);new Thread(shengcan).start();new Thread(xiaofei).start();new Thread(shengcan).start();new Thread(xiaofei).start();}}
开启多线程,运行代码通常是循环结构。只要控制住循环,就能让run()方法结束(run()结束线程就结束)。



------- android培训、java培训、期待与您交流! ----------

0 0
原创粉丝点击