Java多线程
来源:互联网 发布:数控车g73编程实例详解 编辑:程序博客网 时间:2024/06/06 08:22
Runnable
- interface:它是一个接口
- run():它唯一的方法,一个run需要实现的方法,线程的逻辑写在这里
class RunnableDemo implements Runnable{ @override public void run(){ //do something... }}public static void main(String[] args){ Thread t = new Thread(new RunnableDemo()); t.start();}
Thread(单继承方式)
- class:首先它是一个类,它implements Runnable,getName()可以获取它的名字,setName()可以设置它的名字
- run():重写Runnable的方法,它也不实现,只是调用Runnable的run()
- start():the JVM calls the run method of thread.
- sleep():params is millis.
- yeild():the current thread is willing to yeild its current use of processor,the scheduler is free.
- join():wait for this thread to die.
- currentThread():return a reference to the currently executing thread object.
class ThreadDemo extends Thread{ @override public void run(){ //do something }}public static void main(String[] args){ Thread t = new ThreadDemo(); t.start();}
Synchronized
synchronized(object){}块实现线程互斥
运用Object的wait/notify/notifyAll方法实现线程同步
synchronized(object){ object.wait(); object.notify(); obejct.notifyALl();}
wait():the current thread to wait until another thread invokes the notify() method or the notifyAll() for this object.
synchronized也可用于修饰方式,一个被synchronized修饰的方法,保证了线程的安全性,这个方法没被执行完或者没有调用wait方法之前,其他线程都被阻塞,直到方法执行完或者调用wait方法。
volatile
保证了线程可以正确地读取其他线程写入的数据
原因:线程内存可见性,JMM,happens-before原则
注意:volatile只适用于更改数据不依赖于数据本身,比如boolean变量,温度变量等。
Daemon
守护线程
thread.setDaemon(true)
- 在start()之前设置,否则会抛IllegalThreadStateException。
- 在Daemon线程中产生的新线程也是Daemon的。
- 不是所有的应用都可以分配给Daemon线程来进行服务,比如读写操作或者计算逻辑。因为如果没有线程让守护线程守护的话,Daemon Thread还没来的及进行操作时,Daemon Thread可能随着JVM退出了。
Callable、Futrue、ExecutorService、CountDownLatch
实现Callable接口类
public class TestCallable implements Callable<Integer>{ private CountDownLatch countDownLatch; public TestCallable(CountDownLatch countDownLatch){ this.countDownLatch=countDownLatch; } @Override public Integer call() throws Exception { System.out.println(Thread.currentThread().getName()+"run..."); final long numSleep = 10000; int r = (int)(Math.random()*10); System.out.println("start sleep "+Math.floor(numSleep/1000)+" seconds..."); Thread.sleep(numSleep); System.out.println("wake up!"); countDownLatch.countDown(); System.out.println(Thread.currentThread().getName()+"end!"); return r; }}
测试方法:
public static void main(String[] args) throws InterruptedException, ExecutionException { final int numThread = 3; List<Future<Integer>> futureList = new ArrayList<>(); CountDownLatch countDownLatch = new CountDownLatch(numThread);// ExecutorService pool = Executors.newCachedThreadPool(); ExecutorService pool = Executors.newFixedThreadPool(numThread); for(int i=0;i<numThread;i++){ Callable<Integer> c1 = new TestCallable(countDownLatch); Thread.currentThread().setName("thread-"+i); futureList.add(pool.submit(c1)); } pool.shutdown(); countDownLatch.await(); for (Future<Integer> future : futureList) { if(future.isDone()){ System.out.println(future.get()); }else{ System.out.println("f is running..."); } } }
0 0
- 【Java多线程】多线程死锁
- Java 多线程
- java 多线程
- java多线程
- JAVA多线程
- java多线程
- JAVA多线程
- java多线程
- JAVA 多线程
- Java多线程
- java多线程
- JAVA 多线程
- Java 多线程
- Java 多线程
- java多线程
- Java 多线程
- Java多线程
- java 多线程
- python文件的写入
- 性能测试利器iperf
- wps for Linux
- 工作了
- [LeetCode] Max Sum of Rectangle No Larger Than K
- Java多线程
- Python返回数组(List)长度的方法
- Java经典算法(三)
- Gradle的大型项目常用库和版本管理
- 慕课网Vue.js入门基础学习笔记——vuejs及相关工具介绍
- Windows PE 第八章 延迟加载导入表
- Spring的事务隔离与事务传播
- Java学习历程(6):this关键字理解
- Android学习日记