Results 1 to 5 of 5
- 08-07-2010, 09:45 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 15
- Rep Power
- 0
help regarding synchronized threads
hello all,
i am very new to java, and learning how to make threads.
i have made a program in which i want to show table of any number and counting, simultanously.. as follows...
Java Code:Enter Digits: 5 2 Table Counting 5 2 10 3 15 4
but i am getting output as...
Enter Digits: 5 2
Table
5
10
15
Counting
2
3
4
So plz help me to make this program...
Thnx in advance...:)
This Is My Temp class:
Java Code:import java.util.*; class Temp { Scanner s=new Scanner(System.in); int n; synchronized void getTable() throws Exception { System.out.print("Enter No for table:= "); n=s.nextInt(); if(n<0) { throw new User("a"); } else { for(int i=1;i<=10;i++) { System.out.println(n+"*"+i+"="+n*i); } notify(); } } synchronized void getCount() throws Exception { System.out.print(" Enter Number for counting:= "); n=s.nextInt(); if(n<0) { throw new User(); } else { for(int i=n;i<=100;i++) { System.out.println(" Counting "+i); } } } synchronized void getChar() throws Exception { System.out.print(" Enter Character: "); n=s.nextInt(); if(n<65) { throw new User(); } else { System.out.println(" Your alphabet is: "+(char)n); for(int i=1;i<=(90-n);i++) { System.out.println(" Next alphabets is: "+(char)(n+i)); } } } } [U]This Is My MyThread class:[/U] class MyThread extends Thread { Temp w; MyThread(Temp w) { this.w=w; start(); } public void run() { try { w.getTable(); }catch(Exception e){} RunClass u=new RunClass(); u.Condition(); } }
MYTHREAD1 class:
Java Code:class MyThread1 extends Thread { Temp w; MyThread1(Temp q) { this.w=q; start(); } public void run() { try { w.getCount(); }catch(Exception e){} RunClass u=new RunClass(); u.Condition(); } } [U]MyThread2:[/U] class MyThread2 extends Thread { Temp w; MyThread2(Temp q) { this.w=q; start(); } public void run() { try { w.getChar(); }catch(Exception e){} RunClass u=new RunClass(); u.Condition(); } }
This IS RunClass:
Java Code:import java.util.*; class RunClass { int y; void Condition() { Scanner s=new Scanner(System.in); System.out.print("Enter Your Choice:"); y=s.nextInt(); Temp c=new Temp(); if(y==1) { System.out.println("TABLE "+" COUNTING "+" CHARACTER"); MyThread t=new MyThread(c); } else if(y==2) { System.out.println("TABLE "+" COUNTING "+" CHARACTER"); MyThread1 t1=new MyThread1(c); } else if(y==3) { System.out.println("TABLE "+" COUNTING "+" CHARACTER"); MyThread2 t2=new MyThread2(c); } else { System.out.println(""); System.out.println("----Wrong Choice----"); System.out.println("The Program Will EXIT"); System.exit(0); } } public static void main(String zx[]) throws Exception { RunClass r=new RunClass(); r.Condition(); } }
Last edited by Fubarable; 08-08-2010 at 12:02 PM. Reason: Moderator edit: code tags added
-
- 08-08-2010, 05:49 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 15
- Rep Power
- 0
I have added my code sir...
Plz help me out...:)
-
thanks for adding your code, now please tell us what problems you are having with it.
Also, I have added code tags for readability although it doesn't help much as your code is not formatted but rather is all left justified. I suggest that you format your code and repost it. To learn to use code tags yourself, please read the links in my signature.
Much luck!Last edited by Fubarable; 08-08-2010 at 12:03 PM.
- 08-08-2010, 12:47 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 15
- Rep Power
- 0
I will keep that in mind for the next time...:)
Now plz tell me that how can i get the desired output on consloe...??
i.e i want to the program to firslt ask me for all the 3 inputs.. i.e for table,counting and character..
and then to perform each operation at a time in columns as:
TableCountingCharacter
5[INDENT][INDENT]1a
102b
and so on...
i have tried the command line interface also.. but didn't got the desired output..:(
Similar Threads
-
Synchronized(this)?
By kiza in forum Threads and SynchronizationReplies: 10Last Post: 04-07-2009, 01:20 PM -
[SOLVED] Can two threads call two different synchronized instance methods of an Objec
By piyu.sha in forum Threads and SynchronizationReplies: 3Last Post: 10-06-2008, 12:27 AM -
synchronized
By bugger in forum New To JavaReplies: 2Last Post: 11-28-2007, 10:33 AM -
doubt about synchronized
By simon in forum Advanced JavaReplies: 2Last Post: 08-05-2007, 03:49 AM
Bookmarks