Saturday, August 25, 2012

Multi Threadinging in Java

The Basics

to create a thread-able  program you need the following
 requires
  Runnable interface                -> Runnable Object
  Requires a Thread Manager (in main) -> Thinking to use hash-map to handle this
  Requires Thread Launch -> Start method;




------------------------
-------
--------------------

**********************ugly class notes ignore this is for me only***************

Runnable Object ->(Starter Method) an object that is runnable (multi thread)

public void run() -->launch 2ND thread



Runnable Object is an Object that implements an interface Runnable which contains a single method

Creator of Threads: how to create a thread manager Object


Java Support
Thread (Manager) ||  Runnable




uggg explanation is a mess but here is the a code example



public class Message implements Runnable  {                                                            
                                                                                                                                
private String message;                                                                                     
                                                                                                                                                                                        
public Message (String msg)                                                                             
{                                                                                                                     
message = msg;                                                                                     
                                                                                                               
}                                                                                                                        
                                                                                                                         
@Override                                                                                                       
public void run()                                                                                            
                                                                                                                       
{                                                                                                                      
 while(true)                                                                                                   
 {                                                                                                                  
for (int i = 0 ; i < 100; i++)                                                                       
    System.out.print(message);                                                                       
       //Thread.yield()// TODO Auto-generated method stub                               
                                                                                                                  
   System.out.println();                                                                                      
Thread.yield();                                                                                         
 }                                                                                                                     
}                                                                                                                    
                                                                                                                              
}                                                                                                                              



public class Main {

public static void main(String[] args)
{
//create Runnable objects
Message m1 = new Message("X");
Message m2 = new Message("Y");
Message m3 = new Message("Z");
//Create Thread manager objects
Thread t1 = new Thread (m1);
Thread t2 = new Thread (m2);
Thread t3 = new Thread (m3);
//start threads
t1.start();
t2.start();
t3.start();
for (int i = 0 ; i < 1000; i++)
    System.out.print("M");
// System.out.println(m1);
//System.out.println(m2);
//System.out.println(m3);
}
}



No comments:

Post a Comment