当前位置:网站首页>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
边栏推荐
- Shutter: about inheritedwidget
- Shutter: add gradient stroke to font
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- Solve msvcp120d DLL and msvcr120d DLL missing
- Jsup crawls Baidu Encyclopedia
- OpenGL draws colored triangles
- MySQL uses the method of updating linked tables with update
- (construction notes) ADT and OOP
- 网上炒股开户安不安全?谁给回答一下
- Master and backup role election strategy in kept
猜你喜欢
随机推荐
Socket TCP for network communication (I)
[learning notes] DP status and transfer
Summary of development issues
OpenGL index cache object EBO and lineweight mode
Differences between MySQL Union and union all
为什么我的mysql容器启动不了呢
(construction notes) grasp learning experience
vulnhub之momentum
牛牛的组队竞赛
vulnhub之Ripper
Use of QT OpenGL camera
(构造笔记)GRASP学习心得
Concurrent programming - singleton
Ripper of vulnhub
PHP導出word方法(一mht)
Shardingsphere sub database and sub table < 3 >
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
安裝electron失敗的解决辦法
Unity3d learning notes 5 - create sub mesh
Qt OpenGL 旋转、平移、缩放
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)







