1+ /**
2+ *
3+ */
4+ package Day10 ;
5+
6+ import java .io .FileOutputStream ;
7+ import java .io .ObjectOutputStream ;
8+ import java .util .*;
9+ /**
10+ * @author M.NAVEEN
11+ * RANDOM CODER'S
12+ * Tech/Project Lead Android Club
13+ */
14+ public class Threadss {
15+
16+ public Threadss () {
17+
18+ }
19+
20+ public static void main (String [] args )
21+ { new A ().n =100 ;
22+ A x =new A ();
23+ x .start ();
24+
25+ new B ().n =100 ;
26+ B v =new B ();
27+ v .start ();
28+
29+ C c =new C ();
30+ c .start ();
31+ try {
32+ c .join ();
33+ v .join ();
34+ x .join ();
35+ } catch (InterruptedException e ) {
36+
37+ e .printStackTrace ();
38+ }
39+ //ArrayList finals=new ArrayList();
40+ Set setss =new HashSet ();
41+ for (Object temp :x .arr )
42+ setss .add (temp );
43+ for (Object temp :v .arr )
44+ setss .add (temp );
45+ for (Object temp :c .arr )
46+ setss .add (temp );
47+
48+
49+ System .out .println ("Set---------" +setss );
50+
51+ try {
52+ FileOutputStream f =new FileOutputStream ("Finalthread.ser" );
53+ ObjectOutputStream obj =new ObjectOutputStream (f );
54+ obj .writeObject (setss );
55+ obj .flush ();
56+ obj .close ();
57+ }
58+ catch (Exception e ) {
59+ e .printStackTrace ();
60+ }
61+
62+ }
63+
64+ }
65+
66+ class A extends Thread {
67+ static int n ;
68+ ArrayList <Integer > arr =new ArrayList ();
69+ public void run ()
70+ { Random r =new Random ();
71+
72+ for (int i =0 ;i <n ;i ++)
73+ { int flag =-1 ;
74+ int ran =r .nextInt (1000 )+10 ;
75+ System .out .println (ran );
76+ //Check if prime
77+ for (int j =3 ;j <ran ;j ++)
78+ {
79+ if (ran %j ==0 )
80+ {
81+ flag =0 ;
82+ break ;
83+ }
84+ }
85+ if (flag ==-1 ) {System .out .println ("-------prime :" +ran );
86+ arr .add (ran );
87+
88+ try {wait ();}catch (Exception e ){System .out .println ("Thread A is Waiting " );}
89+
90+ }
91+
92+
93+ }
94+ write ();
95+ }
96+ public void write ()
97+ { System .out .println ("File prime :" +arr );
98+ try {
99+ FileOutputStream f =new FileOutputStream ("Outputthread.ser" );
100+ ObjectOutputStream obj =new ObjectOutputStream (f );
101+ obj .writeObject (arr );
102+ obj .flush ();
103+ obj .close ();
104+ }
105+ catch (Exception e ) {
106+ e .printStackTrace ();
107+ }
108+ }
109+ }
110+
111+ class B extends Thread {
112+ static int n ;
113+ ArrayList <Integer > arr =new ArrayList ();
114+ public void run ()
115+ { Random r =new Random ();
116+
117+ for (int i =0 ;i <n ;i ++)
118+ { int ran =r .nextInt (2000 )+50 ;
119+ if (ran %3 ==0 ) {System .out .println ("Divisble by 3 :" +ran );
120+ arr .add (ran );
121+ try {wait ();}catch (Exception e ){System .out .println ("Thread B is Waiting " );}
122+
123+ }
124+
125+
126+ }
127+ write ();
128+ }
129+ public void write ()
130+ { System .out .println ("Divisble by 3 :" +arr );
131+ try {
132+ FileOutputStream f =new FileOutputStream ("Outputthread2.ser" );
133+ ObjectOutputStream obj =new ObjectOutputStream (f );
134+ obj .writeObject (arr );
135+ obj .flush ();
136+ obj .close ();
137+ }
138+ catch (Exception e ) {
139+ e .printStackTrace ();
140+ }
141+ }
142+ }
143+
144+ class C extends Thread {
145+
146+ ArrayList <Integer > arr =new ArrayList ();
147+ public void run ()
148+ { Random r =new Random ();
149+
150+ for (int i =0 ;i <4 ;i ++)
151+ { int ran =r .nextInt (50 )+1 ;
152+ arr .add (ran );
153+ }
154+ write ();
155+ }
156+ public void write ()
157+ { System .out .println ("Number one two 1-50 :" +arr );
158+ try {
159+ FileOutputStream f =new FileOutputStream ("Outputthread3.ser" );
160+ ObjectOutputStream obj =new ObjectOutputStream (f );
161+ obj .writeObject (arr );
162+ obj .flush ();
163+ obj .close ();
164+ }
165+ catch (Exception e ) {
166+ e .printStackTrace ();
167+ }
168+ }
169+ }
0 commit comments