|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
Java Threads In this tutorial I will explain what threads are and how to use them. Simply put a thread is an instance of the class java.lang.Thread. Creating a thread There are two ways to create a thread.
Implementing a thread by extending the thread interface Making a runnable object Code:
Runnable r = new MyClasToRun(); Now its time to send the reference [/i]r [/i]to a thread so that it can run it. Code:
Thread t = new Thread(r); //Consider this for the example, the others are just to give you the various flavors possible.
Thread t = new Thread();
Thread t = new Thread(Runnable object, String name);
Thread t = new Thread(String Name);
Now its time to start the thread you have created. All you do is simply call the start method of the Thread Class. Code:
t.start(); Code:
public void run()
{
//Do stuff here
}
Code:
t.sleep(1000); But now the question arises, what is the big thing about having a thread instead of simple class. The first thing is, that the overhead involved for the JVM is less for starting a new Thread compared to a new process. Second is, in case you put a thread to sleep, the resources are freed and other threads can use it. So what you have is a time sharing system that splits the time between various threads and you get maximum possible output. The example below will explain to you fully have things work in the world of threads. Code:
class DoAllWork implements Runnable
{
public void run()
{
for(int i=0; i<5 ;i++)
{
System.out.println(i+" Running thread : "+Thread.currentThread().getName());
try
{
Thread.sleep(1000);//Puts the thread to sleep for 1 second.
}
catch (InterruptedException e) {
e.printStackTrace();
}}}}
public class ThreadsEx {
public static void main(String[] args) {
Runnable doWork = new DoAllWork(); //Create a reference to the to the DoAllWork class.
Thread t1 = new Thread(doWork,"The first");
Thread t2 = new Thread(doWork,"The second");
t1.start();
t2.start();
}}
Code:
Output 0 Running thread : The first 0 Running thread : The second 1 Running thread : The first 1 Running thread : The second 2 Running thread : The first 2 Running thread : The second 3 Running thread : The first 3 Running thread : The second 4 Running thread : The first 4 Running thread : The second
![]()
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog Post a job on our freelance section! Paste between computers/devices and Collaborate on Code!. |
![]() |
| Tags |
| threads |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Starting Java Using Netbeans | Jordan | Java Tutorials | 0 | 04-05-2008 02:45 PM |
| Java tutorial : my sql with java | Arkie | Java Tutorials | 2 | 04-05-2008 12:51 PM |
| threads in java | stasik | Java Help | 2 | 03-13-2007 03:34 PM |
| John's Java Tutorial Index | John | Java Tutorials | 0 | 01-11-2007 03:05 PM |
Algorithms and Data Structures
Programming Language Popularity
Code Collaboration
Podnet IRC Network
AmpHosted
Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%