Creating threads in Java
2012-05-14 21:25
337 查看
There are in general two ways to create and start a new thread in Java.
1. extend the Thread class and override the run() method
2. implement the Runnable interface
Then how to start a new thread
For the first way
For the second way
In fact, if we check the source of the Thread class, it will be something like this
Note: Please do not directly call thread.run() or runnable.run() if you expect some parallel execution. As the direct calls will make the code synchronously executed in the current thread, in fact the MyThread and MyRunnable are simply two classes with nothing
special. There are some pieces of code in the Thread.start() which can magically create new threads.
1. extend the Thread class and override the run() method
public class MyThread extends Thread{ @Override public void run(){ //define your task here } }
2. implement the Runnable interface
public class MyRunnable implements Runnable{ @Override public void run(){ //define your task here } }
Then how to start a new thread
For the first way
MyThread thread = new MyThread(); thread.start(); //start will eventually call the run() method of the Thread instance
For the second way
MyRunnable runnable = new MyRunnable(); new Thread(runnable).start(); //start will eventually call the run() method of the MyRunnable instance
In fact, if we check the source of the Thread class, it will be something like this
public class Thread implements Runnable{ private Runnable target; public Thread(Runnable tar){ this.target = tar; } public void start(){ // some magic code which will eventually call run() } public void run(){ if(this.target != null){ this.target.run(); } } }Now we can understand the two ways of creating threads, the first way simply uses runtime polymorphism, and the second way is even more straightforward.
Note: Please do not directly call thread.run() or runnable.run() if you expect some parallel execution. As the direct calls will make the code synchronously executed in the current thread, in fact the MyThread and MyRunnable are simply two classes with nothing
special. There are some pieces of code in the Thread.start() which can magically create new threads.
相关文章推荐
- Synchronizing threads in Java
- Creating your first Web Service provider and consumer in LotusScript and Java.
- Process of creating an object in Java
- Core Java Tutorial -- Threads in Java
- Creating Web Services (Bottom Up) in Eclipse Java EE IDE for Web Developers
- 5 Ways of Creating a Stream in Java 8
- 2.Creating WebSocket Applications in the Java EE Platform
- 4 ways to do concurrency in Java: Threads, Executors, ForkJoin and Actors
- Creating and deleting threads dynamically in eCos
- Creating MTS ActiveX Object in Java
- java.lang.ExceptionInInitializerError BeanCreationException: Error creating bean with name 'TestServ
- 知识库--Creating Transaction in Java Using Akka(124)
- Creating Shazam in Java
- Synchronizing threads in Android/Java
- Creating InetAddress object in Java
- Spark - A tiny Sinatra inspired framework for creating web applications in Java 8 with minimal effor
- Threads in Java
- When creating mex in MATLAB: error C3861: 'fmax': identifier not found
- 《Thinking in Java》学习笔记(一)——对象导论
- QTP java add-in与现有的jre环境冲突