当前位置:网站首页>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
边栏推荐
- Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
- Solution à la défaillance de l'installation d'Electron
- pragma-pack语法与使用
- SLF4J 日志门面
- Wrong arrangement (lottery, email)
- Qt+vtk+occt reading iges/step model
- Shutter: add gradient stroke to font
- 网络通讯之Socket-Tcp(一)
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
- Flutter Widget : Flow
猜你喜欢

牛牛的组队竞赛

Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022

vulnhub之raven2

vulnhub之pyexp

为什么我的mysql容器启动不了呢

【mysql专项】读锁和写锁

ArcGIS application (XXI) ArcMap method of deleting layer specified features

Vulnhub's Tomato (tomato)

QT OpenGL rotate, pan, zoom

vulnhub之GeminiInc v2
随机推荐
Duplicate numbers in the array of sword finger offer 03
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
Socket TCP for network communication (I)
Simple factory and factory method mode
为什么我的mysql容器启动不了呢
vulnhub之Nagini
Redis 笔记 01:入门篇
安裝electron失敗的解决辦法
【mysql官方文档】死锁
(construction notes) grasp learning experience
4000 word super detailed pointer
Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
PHP get the file list and folder list under the folder
Dart: view the dill compiled code file
Develop plug-ins for idea
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
CGroup introduction
Cacti monitors redis implementation process
Differences between MySQL Union and union all
Qt OpenGL 纹理贴图