JAVA多线程基础

一.进程和线程概念

进程:程序运行的基本单元,进程包含线程
线程:是CPU调度和分派的基本单位,它是比进程更小的能独立运 行的基本单位

二.线程的实现方式

  1. 继承Thread类
public class ThreadTest extends Thread {  
  public void run() {  
   System.out.println("ThreadTest extends Thread");  
  }  
}  

线程启动

ThreadTest thread = new ThreadTest ();
thread .start()
  1. 实现Runnable接口
public class ThreadTest  implements Runnable {  
  public void run() {  
   System.out.println("ThreadTest  implements Runnable");  
  }  
}  

线程启动

ThreadTest myThread = new ThreadTest();
Thread thread = new Thread(myThread);
thread.start();

三.线程的状态

线程的状态可分为五个状态:新建(new)、就绪(runnable)、运行(running)、等待/阻塞/睡眠(wait/blocked/sleep)、死亡(dead)

下图是线程状态图(图片转自https://my.oschina.net/mingdongcheng/blog/139263)

这里写图片描述

四.线程状态切换

  1. 新建:表示新建一个线程对象
  2. 就绪:线程创建后,就可以调用start方法,但是不立即执行,而是该进程在可运行进程池中,等待CPU调度使用
  3. 运行:线程获得了cpu 时间片(timeslice),即可执行代码
  4. 等待:线程在运行中,调用的wait方法,当前线程进入等待队列;线程等待唤醒,需要调用notify/notifyAll
  5. 阻塞:调用sleep和join都会是当前线程阻塞;sleep()在时间结束后,当前线程又回到就绪状态,等待CPU调度使用,join需要等调用join的线程执行完后,回到就绪状态,等待CPU调度使用
  6. 死亡:线程run()、main() 方法执行结束,或者因异常退出了run()方法,线程整个生命周期结束

注:调用yield()会让当前线程回到就绪状态,让出cpu时间,让优先级高的线程执行


五.线程通信

这里讲wait/notify的使用

public class ProducerConsumerInJava { 
    public static void main(String args[]) { 
        System.out.println("How to use wait and notify method in Java"); 
        System.out.println("Solving Producer Consumper Problem"); 
        Queue&lt buffer = new LinkedList(); 
        int maxSize = 10; 
        Thread producer = new Producer(buffer, maxSize, "PRODUCER"); 
        Thread consumer = new Consumer(buffer, maxSize, "CONSUMER"); 
        producer.start(); 
        consumer.start(); 
        } 
    } 


class Producer extends Thread { 
        private Queue queue; 
        private int maxSize; 
        public Producer(Queue queue, int maxSize, String name){ 
            super(name);
            this.queue = queue; 
            this.maxSize = maxSize; 
        } 
        @Override public void run() 
        { 
            while (true) 
                { 
                    synchronized (queue) { 
                        while (queue.size() == maxSize) { 
                            try { 
                                System.out .println("Queue is full, " + "Producer thread waiting for " + "consumer to take something from queue"); 
                                queue.wait(); 
                            } catch (Exception ex) { 
                                ex.printStackTrace(); } 
                            } 
                            Random random = new Random(); 
                            int i = random.nextInt(); 
                            System.out.println("Producing value : " + i); 
queue.add(i); 
queue.notifyAll(); 
                        } 
                    } 
                } 
            }

  class Consumer extends Thread { 
        private Queue queue; 
        private int maxSize; 
        public Consumer(Queue queue, int maxSize, String name){ 
            super(name); 
            this.queue = queue; 
            this.maxSize = maxSize; 
        } 
        @Override public void run() { 
            while (true) { 
                synchronized (queue) { 
                    while (queue.isEmpty()) { 
                        System.out.println("Queue is empty," + "Consumer thread is waiting" + " for producer thread to put something in queue"); 
                        try { 
                            queue.wait(); 
                        } catch (Exception ex) { 
                            ex.printStackTrace(); 
                        } 
                    } 
                    System.out.println("Consuming value : " + queue.remove()); 
                    queue.notifyAll(); 
                } 
            } 
        } 
    }

这里写图片描述

本文程序来自:http://www.importnew.com/16453.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值