Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket, Thread and synchronized

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have a problem that I hope some of you can help me with.
I have a program that gets messages with socket communication. I want each message to be in a separate thread and then I'm calling a synchronized method (for test this method only do a System.out.println).
I thought that when you call a synchronized method it would be looked for other threads until the first thread had finished the method. But when I look in my log all threads have started the test method before the first one ends it.
I have included some of the code and the log (I have removed some of the System.out)
Would be very thankful if somebody can help me
/Jenny
// Main method, creates a new thread for each message from Socket
public static void main(String args[])
{
Socket Client;
int i=0;

try
{
ServerSocket serverSocket=new ServerSocket(receiverPortName);
System.out.println("Waiting for message....");

while (true) {
Socket readSocket = serverSocket.accept();
readSocket.setKeepAlive(true);
System.out.println(readSocket.getKeepAlive());
Socket sendSocket=new Socket(sendServerName,sendPortName);
sendSocket.setKeepAlive(true);

new Thread(new ClientServicer(readSocket, sendSocket, i)).start();
i++;
}
}
catch (Exception e)
{System.out.println("error:" + e);}
}
// Run method, only call the test method
public synchronized void run(){
}
try {
test();
} catch(Exception exc){
System.err.println(exc.getMessage());
}

}
}
// test method
// Tells when the method start and stops and
// writes the threads name 20 times
public void test() throws InterruptedException{
System.out.println("*****Test************"+getName());
for (int i=0;i<20;i++){
System.out.println(getName());
}
sleep(10000);
//wait();
System.out.println("*****Test slut************"+getName());
}

// LOG FILE
Waiting for message....Thread-0
*****Test************Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Waiting for message....Thread-2
*****Test************Thread-2
Thread-2
Thread-2
Thread-2
Thread-2
Thread-2
Thread-2
Waiting for message....Thread-4
*****Test************Thread-4
Thread-4
Thread-4
Thread-4
Thread-4
Thread-4
Thread-4
*****Test slut************Thread-0
*****Test slut************Thread-2
*****Test slut************Thread-4
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leaving aside the question of what that message near the end out the output means (!), and also the question of why your design requires only one client to be serviced at a time, but yet in a separate thread for each:
Synchronization implies locking an object. If you call a synchronized method on an object you lock that object, and while that first method is running, no other synchronized method can be called on that same object. But in your program, every time run() is called, it's on a new instance of ClientServicer; i.e., there's a seperate lock for each thread, so there's no interference between threads.
The way you've got this set up, you really need a separate ClientServicer for each thread, so what you can do is to use a single unique object for a lock and use a synchronized block instead of a synchronized method. So what you could do is:
  • Add a private static final member variable of type Object to your class and initialize it with "new Object()"; call this variable LOCK.
  • [list] Make your run() method do something like


    You should now get the behavior you want.
     
    Thomas Paul
    mister krabs
    Ranch Hand
    Posts: 13974
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    *****Test slut************
    ...why your design requires only one client to be serviced at a time,
    Am I going to have move this thread to the adult only forum?
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is that dutch or something? Sounds similar to the german "schluss", which just means "end".
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ilja,
    A slut in (american) english ist eine Schlampe auf Deutsch.
     
    Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
    the new thread boost feature: great for the advertiser and smooth for the coderanch user
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!