Saturday, August 25, 2012

Multi Threadinging in Java

The Basics

to create a thread-able  program you need the following
  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;                                                                                     
public void run()                                                                                            
for (int i = 0 ; i < 100; i++)                                                                       
       //Thread.yield()// TODO Auto-generated method stub                               

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
for (int i = 0 ; i < 1000; i++)
// System.out.println(m1);

No comments:

Post a Comment