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 2 of 2

Thread: synchronized block in java

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

    Default synchronized block in java

    Hi ! .Today i am going to ask on synchronized block.

    the general form of synchronized block is
    class table
    {
    .......

    void printTable(int n)

    {
    synchronized(object) {
    ......
    }
    }
    }

    1)Here object means object of any class.i.e we can lock object of any class besides the object of "table" class.
    2)if we place this in place of object it is possible to lock object of table class
    3)then how can we lock object of a class other than table?can you give example based on below programme?
    4) if we want to access a variable or method from table class we need object of table .
    if we lock object of table class it is not possible to an object of table class to access synchronized block of table class
    from two different places simultaneously.
    but if we lock object of another class,say X,how can the object of class X can access synchronized block of table class?
    because object of class X is not object of table.object of table only can access members of table.

    I think you got my doubt.
    I request you to clarify my doubt based on 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
    500


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: synchronized block in java

    The object you use to lock the block with isn't used to access the method, and in fact, there is nothing "magical" about it, and itcan be any object at all, a String, an array, an Object instance, anything. The object used for locking is often just a token and likely won't be accessing any of the involved code in the synchronized block.

Similar Threads

  1. [SOLVED] Help with SYNCHRONIZED
    By mds1256 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 8th, 2012, 10:44 AM
  2. [SOLVED] New to java, need to know how to incorparate system time into a block of code.
    By lostbit in forum Java Theory & Questions
    Replies: 5
    Last Post: September 29th, 2011, 07:46 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. Java sliding block puzzle issues
    By Vesper in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 11th, 2011, 07:30 AM
  5. Creating Block cipher without the use of inbuilt java funcs
    By fortune2k in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 15th, 2010, 09:43 AM