Showing posts with label runnable. Show all posts
Showing posts with label runnable. Show all posts
Friday, June 6, 2014
Best Example Code for synchronized method
Thread_1 0
Thread_2 0
public class SynchronizedTest {
public class MyCounter {
// put synchronized before void for output two
public class Thread1 extends Thread {
public class Thread2 extends Thread {
Labels:
multi-threading,
runnable,
synchronization,
synchronized,
thread
Simple Runnable Implementation
Simple Thread Sample using Runnable interface Implementation
Here the sample code explains how to implement Runnable interface to create threads in Java. Please see previous post to know How to create threads by extending Thread class.
class 1: This MyPrinter is class, has method print() which will be called from multiple threads and is shared across multiple threads.
public class MyPrinter {public void print(String pName,int i)
{
System.out.println(pName+"_"+i);
}
}
class 2: This is the class which implements Runnable interface and overrides the run methods, the job/task you want to perform in thread should be put inside the methods run().
public class Runnable1 implements Runnable {private MyPrinter printer;
Runnable1(MyPrinter printer) {
this.printer = printer;
}
public void run() {
int i = 0;
while (i < 200) {
printer.print("Runnable_1", i);
i++;
}
}
}
class 2: This is the another same as above class which implements Runnable interface and overrides the run methods, the another job/task you want to perform in thread should be put inside the methods run().
public class Runnable2 implements Runnable {private MyPrinter printer;
Runnable2(MyPrinter printer) {
this.printer = printer;
}
public void run() {
int i = 0;
while (i < 200) {
printer.print("Runnable_2", i);
i++;
}
}
}
class 4: The main class to be executed is described here. Please see the difference here, unlike instantiating the Runnable and calling start() method on it, we instantiate Runnable1 and Runnable2 objects and passing those instance as argument to another Thread objects. So these are the real objects.
public class SimpleThreadTest {
public static void main(String[] args) {
MyPrinter p = new MyPrinter();
Runnable1 t1= new Runnable1(p);
Runnable2 t2= new Runnable2(p);
new Thread(t1).start();
new Thread(t2).start();
}
}
Labels:
multi-threading,
run,
runnable,
start,
thread
Simple Thread in Java
Simple Thread using extending Thread and implementing Runnable
Multi-threading means a single code program with different parts executed at same time in parallel, those parts are threads. We here see how to create threads.Class 1: The first class SimpleThreadTest here instantiate a common printer, which receives input strings from multiple threads and prints. And creates two threads for different thread classes Thread1 and Thread2. And starts them both. By running this class you will see the output console with Threads names in mixed order ie., the thread executes parallel with one another same time.
public class SimpleThreadTest {public static void main(String[] args) {
MyPrinter p = new MyPrinter();
Thread1 t1= new Thread1(p);
Thread2 t2= new Thread2(p);
t1.start();
t2.start();
}
}
Class 2: This is the Thread implementation which does some calculation and send the data to be printed to common printer class. The common printer instance is given here as constructor argument.
public class Thread1 extends Thread {private MyPrinter printer;
Thread1(MyPrinter printer) {
this.printer = printer;
}
public void run(){
int i=0;
while( i < 200 ) {
printer.print("Thread 1", i);
i++;
}
}
}
Class 3: This is the (Thread2) Thread implementation which does some calculation and send the data to be printed to common printer class. The common printer instance is given here as constructor argument.
public class Thread2 extends Thread {private MyPrinter printer;
Thread2(MyPrinter printer) {
this.printer = printer;
}
public void run(){
int i=0;
while( i < 200 ) {
printer.print("Thread 2", i);
i++;
}
}
}
Class 4: This is a common printer class has public method print called from different thread instances. Prints the thread name and data(i).
public class MyPrinter {public void print(String pName,int i)
{
System.out.println(pName+"_"+i);
}
}
Subscribe to:
Posts (Atom)