-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo.java
More file actions
93 lines (93 loc) · 1.53 KB
/
ThreadDemo.java
File metadata and controls
93 lines (93 loc) · 1.53 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
class Gen extends Thread
{
int i,array[];
public Gen(int array[])
{
this.array=array;
}
public void run()
{
Random rand=new Random();
System.out.println("Generated Random Numbers");
for(i=0;i<100;i++)
{
array[i]=rand.nextInt(1000);
System.out.println(array[i]);
}
System.out.println("\n");
}
}
class Sort extends Thread
{
private int array[];
public Sort(int array[])
{
this.array=array;
}
public void run()
{
int i;
Arrays.sort(array);
System.out.println("Sorted Array");
for(i=0;i<100;i++)
{
System.out.println(array[i]);
}
System.out.println("\n");
}
}
class Search extends Thread
{
private int array[],ele;
public Search(int array[])
{
this.array=array;
}
public void run()
{
int i,f=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter Element to search");
ele=s.nextInt();
for(i=0;i<100;i++)
{
if(array[i]==ele)
{
f=1;
System.out.println("Element "+ele+" found at index "+i);
break;
}
}
if(f==0)
System.out.println("Element "+ele+" not found");
}
}
class ThreadDemo
{
public static void main(String args[])
{
int i;
int array[]=new int[100];
Gen g=new Gen(array);
Sort s=new Sort(array);
Search sea=new Search(array);
g.start();
try
{
s.sleep(100);
}catch(InterruptedException e){}
s.start();
try
{
sea.sleep(100);
}catch(InterruptedException e){}
sea.start();
/*for(i=10;i<100;i++)
{
System.out.println(array[i]);
}*/
}
}