Results 1 to 5 of 5
  1. #1
    vishalrimt is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default 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
    and so on....


    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 01:02 PM. Reason: Moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by vishalrimt View Post
    So plz help me to make this program...
    First you. Please show us your code and perhaps we can help show you what you're doing wrong.

  3. #3
    vishalrimt is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default

    I have added my code sir...

    Plz help me out...:)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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 01:03 PM.

  5. #5
    vishalrimt is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default

    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

  1. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 02:20 PM
  2. Replies: 3
    Last Post: 10-06-2008, 01:27 AM
  3. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 11:33 AM
  4. doubt about synchronized
    By simon in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •