Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: doubt on synchronized block-2

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default doubt on synchronized block-2

    the general form of synchronized block is
    class table 
    { 
    ......
    .
     void printTable(int n) 
    { 
    synchronized(object)
     { 
    ...... 
    } 
    }
    }
    --------------------------------------------------------
    here object means object of tble class or object of integer or sting... etc classes.
    I want a programme which gets lock on object of string class or object of interger class.I mean the above object should be object of string class or object of integer class.
    can any one give that programe.?
    I request you to give that programme by editing below programme.
    ------------------------------------------------------------
    import java.io.*;
    class table
    {
    void printTable(int n)
    {
    synchronized(this)
    {
    for(int i=1;i<=5;i++)
    {
    System.out.println(n*i);
    try{
    Thread.sleep(500);
    }
    catch(InterruptedException ie)
    {System.out.println(ie);
    }
    }
    }
    }
    }
    class MyThread1 extends Thread
    {
    table t;
    MyThread1(table t)
    {
    this.t=t;
    }
    public void run(){
    t.printTable(5);
    }
    }
    class MyThread2 extends Thread
    {
    table t;
     
    MyThread2 (table t)
    {
    this.t=t;
    }
    public void run()
    {
    t.printTable(100);
    }
    }
    class synchronizedblock1
    {
    public static void main(String args[]
    )
    {
    table t=new table();
    MyThread1 t1=new MyThread1(t);
    MyThread2 t2=new MyThread2(t);
    t1.start();
    t2.start();
    }
    }
    ==========================
     
    output:
     
    5
    10
    15
    20
    25
    100
    200
    300
    400


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: doubt on synchronized block-2

    Have you read the tutorial?
    Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)

    I want a programme which gets lock on object of String class
    String aStr = "";
    ...
    synchronized(aStr) { 
      // have lock on aStr
    }

    If you want anyone to test your code, you need to properly format it. Statements should not all start in the first column. There should be indentations for nested code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: doubt on synchronized block-2

    ALso posted at http://www.javaprogrammingforums.com...html#post85783
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. synchronized block in java
    By me_shankara in forum Threads
    Replies: 1
    Last Post: December 3rd, 2012, 11:04 PM
  2. [SOLVED] Help with SYNCHRONIZED
    By mds1256 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 8th, 2012, 10:44 AM
  3. Synchronized block vs Synchronized method
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:51 AM
  4. Synchronized Collection
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: March 29th, 2011, 12:09 AM
  5. Synchronized Methods problem!
    By RiskyShenanigan in forum AWT / Java Swing
    Replies: 1
    Last Post: November 28th, 2010, 12:04 PM