当前位置:网站首页>Introduction to concurrent programming (I)
Introduction to concurrent programming (I)
2022-07-03 12:11:00 【zwanying】
Introduction to concurrent programming ( One )
Why do I have to be concurrent ?
Improve resource utilization , Speed up the program .
Java Memory model :
The working memory : private Each thread allocates independent memory , Mutual interference
Main memory : All processes share
Thread class [implements Runnable]
There are three ways to create a thread :
Inherit Thread class , rewrite run Method , example .start perform
public class ThreadExtendTest extends Thread{ @Override public void run(){ System.out.println("thread success"); } public static void main(String []args){ new ThreadExtendTest().start(); } }
Realization Runnable Interface , rewrite run Method ,Thread example .start perform
public class ThreadExtendTest implements Runnable{ @Override public void run(){ System.out.println("thread success"); } public static void main(String []args){ new Thread(new ThreadExtendTest()).start(); }
Realization Callable Interface , rewrite call Method , establish FutureTask Get the return value .
public class ThreadCallableTest implements Callable<String>{ @Override public String call(){ return "you are right"; } public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<String> futureTask = new FutureTask<>(new ThreadCallableTest()); Thread thread = new Thread(futureTask); thread.start(); System.out.println(futureTask.get()); } }
Anonymous inner class Realization Runnable Simple version of interface
public class Test { public static void main(String[] args) { Thread t = new Thread(new Runnable() { @Override public void run() { System.out.println("you area success"); } }); t.start(); } }
Thread Methods
run() // Specific operations to be performed start() // perform run() Method sleep() // Sleep n millisecond getName() // Get the thread name setName() // Modify the thread name
边栏推荐
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- [MySQL special] read lock and write lock
- Socket TCP for network communication (I)
- Ripper of vulnhub
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- (构造笔记)GRASP学习心得
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
- Shutter: about inheritedwidget
- [combinatorics] permutation and combination (example of permutation and combination)
- Notes on 32-96 questions of sword finger offer
猜你喜欢
随机推荐
SLF4J 日志门面
OpenGL draws colored triangles
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
PHP导出word方法(一phpword)
PHP导出word方法(一mht)
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Qt+vtk+occt reading iges/step model
Flutter: about monitoring on flutter applications
Vulnhub geminiinc V2
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
Dart: self study system
Experience container in libvirt
Pragma pack syntax and usage
Differences between MySQL Union and union all
laravel 时区问题timezone
(construction notes) learning experience of MIT reading
(构造笔记)ADT与OOP
Oracle advanced (I) realize DMP by expdp impdp command
STL tutorial 10 container commonalities and usage scenarios
Concurrent programming - singleton