JAVA多线程基础
来源:互联网 发布:行装软件 编辑:程序博客网 时间:2024/06/06 01:10
一.进程和线程概念
进程:程序运行的基本单元,进程包含线程
线程:是CPU调度和分派的基本单位,它是比进程更小的能独立运 行的基本单位
二.线程的实现方式
- 继承Thread类
public class ThreadTest extends Thread { public void run() { System.out.println("ThreadTest extends Thread"); } }
线程启动
ThreadTest thread = new ThreadTest ();thread .start()
- 实现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)
四.线程状态切换
- 新建:表示新建一个线程对象
- 就绪:线程创建后,就可以调用start方法,但是不立即执行,而是该进程在可运行进程池中,等待CPU调度使用
- 运行:线程获得了cpu 时间片(timeslice),即可执行代码
- 等待:线程在运行中,调用的wait方法,当前线程进入等待队列;线程等待唤醒,需要调用notify/notifyAll
- 阻塞:调用sleep和join都会是当前线程阻塞;sleep()在时间结束后,当前线程又回到就绪状态,等待CPU调度使用,join需要等调用join的线程执行完后,回到就绪状态,等待CPU调度使用
- 死亡:线程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< 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
阅读全文
0 0
- Java基础/Java多线程
- Java基础-多线程基础篇
- java多线程基础
- Java多线程编程基础
- java多线程开发基础
- Java多线程基础
- Java -- 多线程技术基础
- 【java】多线程基础
- Java基础:多线程
- Java语言基础:多线程
- Java语言基础:多线程
- Java语言基础:多线程
- java多线程基础分析
- Java多线程编程基础
- java 多线程基础
- Java基础_多线程
- Java多线程基础
- java多线程基础
- 如何查看目录下文件的行或者字数等信息
- 关于IO流
- python中的死锁
- 图论--最小费用最大流(MCMF)
- leetcode: 79 Word Search
- JAVA多线程基础
- Web服务器和API接口服务器心跳检查
- DeepLearning笔记-自编码网络
- 零碎的知识点
- Android学习笔记18-聊聊Handler
- Tomcat+Servlet保存Cookie到浏览器
- Java中Class.getXXX()和Class.getDeclaredXXX()的区别
- python中解决死锁的方法
- mysql的优化方法,自己总结的