synchronized statements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Thorner

    synchronized statements

    Hi,

    I am creating a new thread for each of the connections to the server:

    public class Node_C
    {
    ....
    while (listening) {
    Socket client_socket = server_socket.a ccept();
    Node_CThread node = new Node_CThread(cl ient_socket);
    node.start();
    }
    }

    class Node_CThread extends Thread
    {
    private Socket socket = null;

    public Node_CThrad(Soc ket socket) {
    super("Node_CTh read");
    this.socket = socket;
    }

    public void run() {
    ...
    System.out.prin tln("IP address = "...);
    System.out.prin tln(" IPcount = "...);
    System.out.prin tln(" delay = "...);
    }
    }

    Here is what I need to do:
    - I would like to print all the System.out.prin tln() statements that are
    related to each other (as shown above) one right after the other for
    the same thread. That is, I want the System.out.prin tln() statements
    to be printed together for the same thread they belong to.
    But, since I am using threads, each line of of each thread is executed
    at random intervals. Thus I don't know which line of output belongs
    to which thread. Btw, I have already labeled each line of output with
    the corresponding thread label, but the output is so messy.
    - How could I do that?

    I have tried to use synchronized(th is), as well as
    synchronized(Sy stem.out) without any luck. That is, the following code
    did not work:
    public void run() {
    ...
    synchronized(Sy stem.out) {
    System.out.prin tln("IP address = "...);
    System.out.prin tln(" IPcount = "...);
    System.out.prin tln(" delay = "...);
    }
    }

    Any help would be greated appreciated.
    Thanks in advance...
    -John

  • Jonas Kongslund

    #2
    Re: synchronized statements

    John Thorner wrote:[color=blue]
    > - I would like to print all the System.out.prin tln() statements that are
    > related to each other (as shown above) one right after the other for
    > the same thread. That is, I want the System.out.prin tln() statements
    > to be printed together for the same thread they belong to.[/color]

    A quick solution not involving the synchronized keyword is to print only one
    string per thread, i.e. concatenate the strings.

    --
    Jonas Kongslund

    Comment

    • FISH

      #3
      Re: synchronized statements

      Jonas Kongslund <dont@mail.me.a t.all> wrote in message news:<XiYpb.760 3$yq2.1828@news .get2net.dk>...[color=blue]
      > John Thorner wrote:[color=green]
      > > - I would like to print all the System.out.prin tln() statements that are
      > > related to each other (as shown above) one right after the other for
      > > the same thread. That is, I want the System.out.prin tln() statements
      > > to be printed together for the same thread they belong to.[/color]
      >
      > A quick solution not involving the synchronized keyword is to print only one
      > string per thread, i.e. concatenate the strings.[/color]


      This doesn't stop the printing of one set of strings interrupting the
      printing of another set - I don't think System.out has any kind of
      locking mechanism to ensure two overlapping calls to println cannot
      interleave their output on the console. (Unless someone knows better?)

      I'm surprised acquiring the lock on System.out didn't work. At first
      glance I'd expect that to do the job, as all threads which entered that
      synchronized block would be required to own the lock on System.out .
      If this genuinely doesn't work (perhaps there's something lurking within
      System.out.prin tln which releases the lock?) then the alternative is to
      replicate the behaviour with your own object. Just create a singleton
      object (a static member will do) and lock on that instead.


      -FISH- ><>

      Comment

      • Jonas Kongslund

        #4
        Re: synchronized statements

        FISH wrote:[color=blue]
        > This doesn't stop the printing of one set of strings interrupting the
        > printing of another set - I don't think System.out has any kind of
        > locking mechanism to ensure two overlapping calls to println cannot
        > interleave their output on the console. (Unless someone knows better?)[/color]

        Well, it has. See the source for java.io.PrintSt ream.

        --
        Jonas Kongslund

        Comment

        • noel wood

          #5
          Re: synchronized statements

          I'm only a student at uni but here is my contrib to this discussion. When
          you are synchonizing on a thread what you are doing is just synchonizing
          threads of the same instance. Core Web Programming by Marty Hall second
          edition pg 710 suggests that to synchonize multiple instances of a class you
          need to synchonize on a class object or some Arbitary object. Make sure
          that it is the same object across all instances of the class. As a straight
          quote from Marty Hall...
          public class SomeThreadedCla ss extends Thread{
          private static Object lockObject = new object();

          public void doSomeOperation (){
          synchronized(lo ckObject){
          accessSomeShare dObject();
          }
          }
          }
          Noel
          "John Thorner" <zeroknw@yahoo. com> wrote in message
          news:%KXpb.4711 $j_4.1271@laker ead05...[color=blue]
          > Hi,
          >
          > I am creating a new thread for each of the connections to the server:
          >
          > public class Node_C
          > {
          > ...
          > while (listening) {
          > Socket client_socket = server_socket.a ccept();
          > Node_CThread node = new Node_CThread(cl ient_socket);
          > node.start();
          > }
          > }
          >
          > class Node_CThread extends Thread
          > {
          > private Socket socket = null;
          >
          > public Node_CThrad(Soc ket socket) {
          > super("Node_CTh read");
          > this.socket = socket;
          > }
          >
          > public void run() {
          > ...
          > System.out.prin tln("IP address = "...);
          > System.out.prin tln(" IPcount = "...);
          > System.out.prin tln(" delay = "...);
          > }
          > }
          >
          > Here is what I need to do:
          > - I would like to print all the System.out.prin tln() statements that are
          > related to each other (as shown above) one right after the other for
          > the same thread. That is, I want the System.out.prin tln() statements
          > to be printed together for the same thread they belong to.
          > But, since I am using threads, each line of of each thread is executed
          > at random intervals. Thus I don't know which line of output belongs
          > to which thread. Btw, I have already labeled each line of output with
          > the corresponding thread label, but the output is so messy.
          > - How could I do that?
          >
          > I have tried to use synchronized(th is), as well as
          > synchronized(Sy stem.out) without any luck. That is, the following code
          > did not work:
          > public void run() {
          > ...
          > synchronized(Sy stem.out) {
          > System.out.prin tln("IP address = "...);
          > System.out.prin tln(" IPcount = "...);
          > System.out.prin tln(" delay = "...);
          > }
          > }
          >
          > Any help would be greated appreciated.
          > Thanks in advance...
          > -John
          >[/color]


          Comment

          • FISH

            #6
            Re: synchronized statements

            Jonas Kongslund <dont@mail.me.a t.all> wrote in message news:<%ubqb.158 08$%C5.13208@ne ws.get2net.dk>. ..[color=blue]
            > FISH wrote:[color=green]
            > > This doesn't stop the printing of one set of strings interrupting the
            > > printing of another set - I don't think System.out has any kind of
            > > locking mechanism to ensure two overlapping calls to println cannot
            > > interleave their output on the console. (Unless someone knows better?)[/color]
            >
            > Well, it has. See the source for java.io.PrintSt ream.[/color]


            Could have sworn I've seen System.out interleave console output - but
            obviously I was mistaken. Thanks. ;-)

            Anyway, IMHO the best solution is solve the locking mechanism issue, not
            concat'ing a load of strings together, prior to output.

            -FISH- ><>

            Comment

            • Jonas Kongslund

              #7
              Re: synchronized statements

              FISH wrote:[color=blue]
              > Anyway, IMHO the best solution is solve the locking mechanism issue, not
              > concat'ing a load of strings together, prior to output.[/color]

              IMHO I think both solutions have their advantages and disadvantages. Which
              one you want to use really depends on the problem at hand.

              --
              Jonas Kongslund

              Comment

              • Raymond DeCampo

                #8
                Re: synchronized statements

                FISH wrote:[color=blue]
                > Jonas Kongslund <dont@mail.me.a t.all> wrote in message news:<%ubqb.158 08$%C5.13208@ne ws.get2net.dk>. ..
                >[color=green]
                >>FISH wrote:
                >>[color=darkred]
                >>>This doesn't stop the printing of one set of strings interrupting the
                >>>printing of another set - I don't think System.out has any kind of
                >>>locking mechanism to ensure two overlapping calls to println cannot
                >>>interleave their output on the console. (Unless someone knows better?)[/color]
                >>
                >>Well, it has. See the source for java.io.PrintSt ream.[/color]
                >
                >
                >
                > Could have sworn I've seen System.out interleave console output - but
                > obviously I was mistaken. Thanks. ;-)
                >[/color]
                What you probably saw was interleaved output from System.out and System.err.

                Ray

                Comment

                Working...