This is featured post 1 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 2 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 3 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 4 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 5 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.


Custom Search
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thread Synchronization in Java and Android

0 comments

Java is always fun love for me. Thread was the only thing that make feared so study it hard and now want to make sure that it will not happen with any one now. I have discussed about how to create simple Thread. But most important is to Synchronization of two thread.

For example -We have one file. And we have two thread one for reading this file and second for writing file. So in this case if we want to read everything that has to be written by write thread.Then Read thread to wait (if write is running) until Write does not complete it task.

For Thread Synchronization important thing is to note that we can synchronize only on one object.
Although i will write code in android technology but in java and Blackberry its almost same.For this i need to take two thread

Thread 1

private class Thread2 extends Thread{

private SynDemo d;
public Thread2(SynDemo sDemo) {
d=sDemo;
}
@Override
public void run() {
super.run();
d.printNumber("Thread2");
}
}

This Thread will print number from 0 to 2000.Next thread also will doing the same.

Thread 2


private class Thread1 extends Thread{
private SynDemo d;
public Thread1(SynDemo sDemo) {
d=sDemo;
}
@Override
public void run() {
super.run();
d.printNumber("Thread1");
}
}

Now create class on which we have method on which we have to synchronize both Thread.

private class SynDemo {

synchronized void printNumber(String string){
for(int i=0;i<2000;i++){
Log.i(""+string,String.valueOf(i));
}
}
}
Now have some explanation ,Both Thread contain one constructor.In which they have SynDemo object to achieve Note that we can synchronize only on one object.In both constructor we will pass the same object Now look how
SynDemo d=new  SynDemo();  //Create one object and pass same object in both
// thread
Thread1 th=new Thread1(d);
Thread2 th2=new Thread2(d);
th.start();
th2.start();
Final code for android will be like this.Play with it
package com.hb;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SeekBarActivity extends Activity {
/** Called when the activity is first created. */
SeekBar seek;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SynDemo d=new SynDemo();
Thread1 th=new Thread1(d);
Thread2 th2=new Thread2(d);

th.start();
th2.start();
}

private class Thread1 extends Thread{
private SynDemo d;
public Thread1(SynDemo sDemo) {
d=sDemo;
}
@Override
public void run() {
super.run();
d.printNumber("Thread1");
}
}

private class Thread2 extends Thread{

private SynDemo d;
public Thread2(SynDemo sDemo) {
d=sDemo;
}
@Override
public void run() {
super.run();
d.printNumber("Thread2");
}
}
private class SynDemo {

synchronized void printNumber(String string){
for(int i=0;i<2000;i++){
Log.i(""+string,String.valueOf(i));
}
}
}

}

Simple Thread example in java

0 comments

Before posting this example i would to thank Herbert Shield


The simple example shown in full on the previous page defines two classes: SimpleThread and TwoThreadsTest. Let's begin our exploration of the application with the SimpleThread class: a subclass of the Thread class that is provided by the java.lang package.
class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
The first method in the SimpleThread class is a constructor that takes a String as its only argument. This constructor is implemented by calling a superclass constructor and is only interesting to us because it sets the Thread's name which is used later in the program.
The next method in the SimpleThread class is the run() method. The run() method is the heart of any Thread--it's where the action of the Thread takes place. The run() method of the SimpleThread class contains a for loop that iterates ten times. In each iteration the method displays the iteration number and the name of the Thread then sleeps for a random interval between 0 and 1 second. After the loop has finished, the run() method prints "DONE!" along with the name of the thread. That's it for the SimpleThread class.
The TwoThreadsTest class provides a main() method that creates two SimpleThread threads: one is named "Jamaica" and the other is named "Fiji". (If you can't decide on where to go for vacation you can use this program to help you decide--go to the island whose thread prints "DONE!" first.)
class TwoThreadsTest {
public static void main (String args[]) {
new SimpleThread("Jamaica").start();
new SimpleThread("Fiji").start();
}
}
The main() method also starts each thread immediately following its construction by calling the start() method. To save you from typing in this program, click here for the source code to the SimpleThreadclass and here for the source code to the TwoThreadsTest program. Compile and run the program and watch your vacation fate unfold. You should see output similar to the following:
0 Jamaica
0 Fiji
1 Fiji
1 Jamaica
2 Jamaica
2 Fiji
3 Fiji
3 Jamaica
4 Jamaica
4 Fiji
5 Jamaica
5 Fiji
6 Fiji
6 Jamaica
7 Jamaica
7 Fiji
8 Fiji
9 Fiji
8 Jamaica
DONE! Fiji
9 Jamaica
DONE! Jamaica
(Looks like I'm going to Fiji!!) Notice how the output from each thread is intermingled with the output from the other. This is due to the fact that both SimpleThread threads are running concurrently thus bothrun() methods are running at the same time and each thread is displaying its output at the same time as the other.
Try This: Change the main program so that it creates a third thread with the name "Bora Bora". Compile and run the program again. Does this change your island of choice for your vacation? Here's the code for the new main program which we renamed to ThreeThreadsTest.