Friday, 27 April 2018


synchronization in multithreading.........................
=========================================================================
package multithreading;

public class CharInt {


synchronized public void printChar()
{
for(int i=65;i<=75;i++)
{
System.out.print((char)i+" ");
}
}


synchronized public void numPrint()
{
for(int i=65;i<=75;i++)
{
System.out.print(i+" ");
}
}

}


===============================================================================================


package multithreading;

public class CharDemo extends Thread {

CharInt ci;


public CharDemo(CharInt ci) {
super();
this.ci = ci;
}


public void run()
{
ci.printChar();
}



}

===============================================================================================

package multithreading;

public class IntDemo extends Thread {

CharInt ci;

public IntDemo(CharInt ci) {
super();
this.ci = ci;
}


public void run()
{
ci.numPrint();
}

}

=====================================================================================================

package multithreading;

public class CharIntMain {
public static void main(String[] args) {
CharInt ci=new CharInt();
CharDemo cd=new CharDemo(ci);
IntDemo id=new IntDemo(ci);
cd.start();
id.start();
}

}

===============================================================================================
without synchronization....................

o/p:------------
A B C D 65 66 67 68 69 70 71 72 73 74 75 E F G H I J K

with synchronization....................

o/p:------------

A B C D E F G H I J K 65 66 67 68 69 70 71 72 73 74 75

1 comment: