• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Socket, Thread and synchronized

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    mister krabs
    Posts: 13974
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is that dutch or something? Sounds similar to the german "schluss", which just means "end".
     
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ilja,
    A slut in (american) english ist eine Schlampe auf Deutsch.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic