Results 1 to 3 of 3
  1. #1
    me_shankara is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Is it possible to acceessdifferent parts of class by different threads?

    Hi !my doubts are based on below programme.
    ----------------------------------------------------------------
    class C implements Runnable
    {
    public void f1()
    {
    System.out.println("Hi");
    }
    public void run()
    {
    f1();
    }
    synchronized void f2()
    {
    System.out.println("Hello");
    }
    }
    class synchronized
    {
    public static void main (String args[])
    {
    C c1=new C();
    Thread t1=new Thread(c1);
    Thread t2=new Thread(c1);
    t1.start();
    t2.start();
    }
    }

    -------------------------------------------

    Here c1 is object of class C.
    I have accessed method f1 of object c1 by threads t1 and t2.
    My Doubt:
    I want to access method f1 by thread t1 and f2 by thread t2.
    Is it possible?
    If possible please tell me

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,945
    Rep Power
    4

    Default Re: Is it possible to acceessdifferent parts of class by different threads?

    Yes. Although I have structured my method a little differently.

    Java Code:
    class C  {
       public void f1() {
          System.out.println("Hi");
       }
    
       public void f2() {
          System.out.println("Hello");
       }
    }
    
    public class MyClass {
       public static void main(String args[]) {
          final C c1 = new C();
          Runnable r1 = new Runnable() {
             public void run() {
                System.out.println(Thread.currentThread().getName());
                c1.f1();
             }
          };
          Runnable r2 = new Runnable() {
             public void run() {
                System.out.println(Thread.currentThread().getName());
                c1.f2();
             }
          };
          new Thread(r1, "Thread 1").start();
          new Thread(r2, "Thread 2").start();
       }
    }
    Also, the synchronize is not necessary since in this case only one thread is accessing each method. Also note that they may not always print in the same order.

    Regards,
    Jim
    Last edited by jim829; 05-20-2013 at 08:10 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Is it possible to acceessdifferent parts of class by different threads?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Rerunning Parts of Code
    By philip1597 in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 07:09 AM
  2. Set parts of text to italics
    By Blondedude092 in forum New To Java
    Replies: 12
    Last Post: 11-27-2012, 12:26 AM
  3. Using threads for animation in a non-main class.
    By wikemol in forum AWT / Swing
    Replies: 4
    Last Post: 01-15-2012, 05:20 PM
  4. Replies: 0
    Last Post: 09-12-2011, 01:06 PM
  5. several threads for different parts
    By Itun in forum AWT / Swing
    Replies: 2
    Last Post: 03-20-2011, 11:18 PM

Posting Permissions

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