Java program for creating multiple threads

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class NewThread implements Runnable
{
 String name;
 Thread t;
 NewThread(String threadname)
 {
  name=threadname;
  t=new Thread(this,name);
  System.out.println("New Thread:"+t);
  t.start();
 }
 public void run()
 {
  try
  {
   for(int i=5;i>0;i--)
   {
    System.out.println(name+ ":"+i);
    Thread.sleep(1000);
   }
  }
  catch(InterruptedException e)
  {
   System.out.println(name+" Interrupted");
  }
  System.out.println(name+" exiting");
 }
}
class MultiThreadDemo
{
 
public static void main(String[] args)
 {
  new NewThread("One");
  new NewThread("Two");
  new NewThread("Three");
  try
  {
   Thread.sleep(10000);
  }
  catch(InterruptedException e)
  {
   System.out.println("Main Thread Interrupted");
  }
  System.out.println("Main Thread Exiting");
 }
}
Output:
New Thread :Thread[One,5,main]
New Thread : Thread[Two,5,main]
One:5
Two:5
New Thread : Thread[Three,5,main]
Three:5
One:4
Three:4
Two:4
One:3
Three:3
Two:3
One:2
Three:2
Two:2
One:1
Three:1
Two:1
One exiting
Three exiting
Two exiting
Main Thread Exiting

Java program that finds the area of a circle using Client-Server network

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.net.*;
import java.io.*;
public class  server
{
 public static void main(String args[]) throws Exception
 {
  ServerSocket ss=new ServerSocket(2000);
  Socket s=ss.accept();
  BufferedReader br=new BufferedReader(newInputStreamReader(s.getInputStream()));
  double rad,area;
  String result;
  rad=Double.parseDouble(br.readLine());
  System.out.println("From Client : "+rad);
  area=Math.PI*rad*rad;
  result="Area is "+area;
  PrintStream ps=new PrintStream(s.getOutputStream());
  ps.println(result);
  br.close();
  ps.close();
  s.close();
  ss.close();
 }
}
public class  client
{
 public static void main(String args[]) throws Exception
 {
 
  Socket s=new Socket("192.168.0.19",2000);
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  String rad;
  System.out.println("Enter radius of the circle ");
  rad=br.readLine();
  PrintStream ps=new PrintStream(s.getOutputStream());
  ps.println(rad);
  BufferedReader fs=newBufferedReader(new InputStreamReader(s.getInputStream()));
  String result=fs.readLine();
  System.out.println("From Server : "+result);
  br.close();
  fs.close();
  ps.close();
  s.close();
 }
}
Output:
Java client
Enter radius of the circle
10
From Server: Area is 314.1341345

Java program for implentation of consumer problem using inter thread communication

Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Q
{
 int n;
 boolean valueSet=false;
 synchronized int get()
 {
  if(!valueSet)
  try
  {
   wait();
  }
  catch(InterruptedException e)
  {
   System.out.println("Interrupted Exception caught");
  }
  System.out.println("Got:"+n);
  valueSet=false;
  notify();
  return n;
 }
 synchronized void put(int n)
 {
  if(valueSet)
  try
  {
   wait();
  }
  catch(InterruptedException e)
  {
   System.out.println("Interrupted Exception caught");
  }
  this.n=n;
  valueSet=true;
  System.out.println("Put:"+n);
  notify();
 }
}
class Producer implements Runnable
{
 Q q;
 Producer(Q q)
 {
  this.q=q;
  new Thread(this,"Producer").start();
 }
 public void run()
 {
  int i=0;
  while(true)
  {
   q.put(i++);
  }
 }
}
class Consumer implements Runnable
{
 Q q;
 Consumer(Q q)
 {
  this.q=q;
  new Thread(this,"Consumer").start();
 }
 public void run()
 {
  while(true)
  {
   q.get();
  }
 }
}
class ProdCons
{
 public static void main(String[] args)
 {
  Q q=new Q();
  new Producer(q);
  new Consumer(q);
  System.out.println("Press Control-c to stop");
 }
}
Output:
Put:1
Got:1
Put:2
Got:2
Put:3
Got:3
Put:4
Got:4
Put:5
Got:5
Put:6
Got:6
Put:7
Got:7
Put:8
Got:8
Put:9
Got:9
Put:10
Got:10
Put:11
Got:11
Put:12
Got:12
Put:13
Got:13
Put:14
Got:14

Java program that illustrates how run time polymorphism is achieved

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class figure
{
 double d1,d2;
 figure(double a,double b)
 {
  d1=a;
  d2=b;
 }
 double area()
 {
  System.out.println("Area of the figure");
  return 0;
 }
}
class rectangle extends figure
{
 rectangle(double a,double b)
 {
  super(a,b);
 }
 double area()
 {
  System.out.println("Area of rectangle");
  return d1*d2;
 }
}
class triangle extends figure
{
 triangle(double a,double b)
 {
  super(a,b);
 }
 double area()
 {
  System.out.println("Area of triangle");
  return d1*d2/2;
 }
}
class runpoly
{
 public static void main(String[] args)
 {
  figure f=new figure(45,6);
  rectangle r=new rectangle(10,30);
  triangle t=new triangle(10,20);
  figure a;
  a=f;
  System.out.println(a.area());
  a=r;
  System.out.println(a.area());
  a=t;
  System.out.println(a.area());
 }
}
Output:
Area of figure
0.0
Area of rectangle
300.0
Area of triangle
100.0

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls