多线程

作者: 玄薛烨 | 来源:发表于2016-08-05 10:28 被阅读18次

什么是多线程?多线程与一般的程序不同,一般的程序都是单线顺序执行的,而多线程则是多个程序同时执行。一般的程序就是一个干十个人的活,而多线程则是十个人干十个人的活;


image.png
  • 多线程的两种实现方法
    package code;

    public class Textthread {
    
    public static void main(String[] args) {
      Thread coding = new Coding();
      Thread print = new Thread(new Print());
      
      coding.start();
      print.start();
      System.out.println("你好啊");
      
     }
    }
    
    class Coding extends Thread {
    public void run() {
      for (int i = 0; i < 100; i++) {
          System.out.println("小明打了" + i + "行代码");
          try {
              Thread.sleep(2);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
    }
    }
    
    class Print implements Runnable {
       public void run() {
        long startTime = System.currentTimeMillis();
           for (int i = 0; i < 200; i++) {
             System.out.println("小明打印了" + i + "页材料");
                try {
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            long endTime = System.currentTimeMillis();
       System.out.println("共用时" + (endTime -startTime) + "ms");
    }
     }
    
  • 线程的等待

           public class ProducerAndConsumer {
    
              public static void main(String[] args) {
                   Box box = new Box();
                   Thread producer = new Producer(box);
                   Thread consumer = new Consumer(box);
                   producer.start();
                  consumer.start();
               }
           }
    
           class Box{
              public int boxValue = 0;
         }
    
    class Producer extends Thread{
        Box box;
        public Producer(Box box){
            this.box = box;
        }
        public void run(){
            for(int i = 1; i<6; i++){
               synchronized(box){
                 while(box.boxValue!=0){
                    System.out.println("Producer: 盒子非空,进入等待队列");
                     try {
                        box.wait();
                    } catch (InterruptedException e) {
                         e.printStackTrace();
                    }
                }
                box.boxValue = i;
                System.out.println("盒子中放入了"+i+",并通知其他等待者");
                 box.notify();
               }
             }
        }
     }
    
    
    class Consumer extends Thread{
          Box box;
        public Consumer(Box box){
            this.box = box;
        }
        public void run(){
            for(int i = 1; i<6; i++){
               synchronized(box){
                while(box.boxValue==0){
                    System.out.println("Consumer: 盒子为空,进入等待队列");
                     try {
                        box.wait();
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                box.boxValue -= i;
                System.out.println("从盒子取出了"+i+",并通知其他等待者");
                 box.notify();
              }
            }
        }
    }
    
  • 锁的实现
    public class ThreadAccount {

         public static void main(String[] args) {
             BankAccount account = new BankAccount();
      Thread husband = new BankThread(account);
      Thread wife = new BankThread(account);
      husband.start();
      wife.start();
      
         }
     }
    
    
    class BankAccount {
         private double balance = 1000;
         public boolean isSuccess(double add){
             if(add<=0){
                 return false;
             }else{
                 synchronized(this){
                     System.out.println("当前的余额为:"+balance);
                    try {
                        Thread.sleep(3);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                    }
                   balance += add;
                     System.out.println("新的余额为:"+balance);
                 }
                 return true;
             }
        }
     }
    class BankThread extends Thread{
        BankAccount account;
        public BankThread(BankAccount account){
            this.account = account;
        }
        public void run(){
            account.isSuccess(200);
        }
    }
    
  • 死锁

    public class DeadLock {
    
        public static void main(String[] args) {
            Object lock1 = new Object();
            Object lock2 = new Object();
            Thread thread1 = new ThreadA(lock1,lock2);
            Thread thread2 = new ThreadB(lock1,lock2);
             thread1.start();
             thread2.start();
         }
    }
    
    class ThreadA extends Thread{
         Object lock1;
         Object lock2;
         public ThreadA(Object lock1, Object lock2){
             this.lock1 = lock1;
            this.lock2 = lock2;
        }
        public void run(){
            synchronized(lock1){
                 System.out.println("A拿到了lock1");
                 try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                 }
                synchronized(lock2){
                     System.out.println("A拿到了lock1和lock2");
                }
            }
        }
    }
    
    
    
    class ThreadB extends Thread{
        Object lock1;
        Object lock2;
        public ThreadB(Object lock1, Object lock2){
            this.lock1 = lock1;
            this.lock2 = lock2;
        }
        public void run(){
            synchronized(lock2){
                System.out.println("B拿到了lock2");
                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized(lock1){
                     System.out.println("B拿到了lock1和lock2");
                }
           }
         }
    }

相关文章

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程: GCD

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程基础

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • iOS进阶之多线程管理(GCD、RunLoop、pthread、

    深入理解RunLoopiOS多线程--彻底学会多线程之『GCD』iOS多线程--彻底学会多线程之『pthread、...

  • iOS多线程相关面试题

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • 多线程之--NSOperation

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • iOS多线程之--NSThread

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

网友评论

      本文标题:多线程

      本文链接:https://www.haomeiwen.com/subject/kpehsttx.html