0% found this document useful (0 votes)
3 views5 pages

Java - Multithreading

The document contains Java code examples demonstrating synchronized threads and inter-thread communication. It includes classes for drawing a pyramid with synchronized methods, implementing the Runnable interface, and managing input/output for a circle's radius using wait and notify. The main classes showcase the creation and execution of threads to illustrate these concepts.

Uploaded by

oraonladeshwar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java - Multithreading

The document contains Java code examples demonstrating synchronized threads and inter-thread communication. It includes classes for drawing a pyramid with synchronized methods, implementing the Runnable interface, and managing input/output for a circle's radius using wait and notify. The main classes showcase the creation and execution of threads to illustrate these concepts.

Uploaded by

oraonladeshwar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

synchronized Thread

package synchthreads;
public class Pyramid {
​ synchronized void draw_pyramid(char
ch) {
​ ​ for(int i=0;i<10;i+=2) {
​ ​ ​ for(int k=10-i;k>0;k-=2) {
​ ​ ​ ​ [Link](" ");
​ ​ ​ }
​ ​ ​ for( int j=0;j<=i;j++) {
​ ​ ​ ​ [Link](ch);
​ ​ ​ }
​ ​ ​ [Link]();
​ ​ }
​ }
}

package synchthreads;
public class A extends Thread{
​ Pyramid p;
​ A(Pyramid p){
​ ​ this.p=p;
​ }
​ public void run() {
​ ​ p.draw_pyramid('1');
​ }
}

package synchthreads;
public class B extends Thread{
​ Pyramid p;
​ B(Pyramid p){
​ ​ this.p=p;
​ }
​ public void run() {
​ ​ p.draw_pyramid('@');
​ }
}

package synchthreads;
public class SynchTest{
​ public static void main(String[] args)
{
​ ​ Pyramid pobj=new Pyramid();
​ ​ A threadA=new A(pobj);
​ ​ B threadB=new B(pobj);
​ ​ [Link]();
​ ​ [Link]();
​ }
}
—-----------------------------------------------------------------------------------------------------------------------

Runnable Interface in a synchronised Thread

package synchthreadsrunableinterface;
public class X implements Runnable{
​ @Override
​ public void run() {
​ ​ // TODO Auto-generated method stub
​ ​
​ ​ for(int i=1; i<=10; i++) {
​ ​ ​ [Link]("\t Thread X
: "+i);
​ ​ }
​ ​ [Link]("End of Thread
X");
​ }
}

package synchthreadsrunableinterface;
public class RunnableTest {
​ public static void main(String[] args)
{
​ ​ X runnable =new X();
​ ​ Thread threadx=new
Thread(runnable);
​ ​ [Link]();
​ ​ [Link]("End of Main
Thread");
​ }
}
—-----------------------------------------------------------------------------------------------------------------------

Inter-thread communication

package interthreadcommuniction;
public class Circle {
float radius=0.0f;
​ synchronized void output() {
​ ​ [Link]("output method
invoked for displaying area of circle");
​ ​ if(radius==0.0) {
​ ​ ​ [Link]("waitting
for input of radius");
​ ​ ​ try {
​ ​ ​ ​ wait();
​ ​ ​ }
​ ​ ​ catch(Exception e) {​ ​
​ ​ ​ }
​ ​ }
​ ​
[Link]("Area="+3.14*radius*ra
dius);
​ }
​ synchronized void input(float r) {
​ ​ [Link]("Inputting
radius....");
​ ​ radius=r;
​ ​ [Link]("radius value
received");
​ ​ notify();
​ }
}
package interthreadcommuniction;
public class TestCircle {
​ public static void main(String[] args)
{
​ ​ final Circle c=new Circle();
​ ​ new Thread() {
​ ​ ​ public void run() {
​ ​ ​ ​ [Link]();
​ ​ ​ }
​ ​ }.start();
​ ​ new Thread()
​ ​ {
​ ​ ​ public void run() {
​ ​ ​ ​ [Link](2.5f);
​ ​ ​ }
​ ​ }.start();
​ }
}

You might also like