my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Java multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Java multithreading" Watch "Java multithreading" New topic
Author

Java multithreading

Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
HI guys,
I am implementing a simple program wherein the server listens on a socket and accepts connection from the client. It places the socket in an Arraylist , on which threads from the threadpool are waiting. Whenever I add a socket into the arraylist, i notify one of the waiting threads to remove the socket from the arraylist, But whenever I try to do it, none of the threads wakes up and handles the socket.

SERVER CODE

public class ServerConnection
{
static List<Socket> requestList= Collections.synchronizedList(new ArrayList<Socket>());

public static void main(String [] args) throws IOException
{
ServerSocket serverSock=new ServerSocket(8080);
while(true)
{
try {
//adding the accepted connection to the queue
requestList.add(serverSock.accept());
synchronized(requestList)
{
requestList.notify();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


THE CODE FOR THREADPOOL IS :

public class ThreadPool extends Thread
{
static ThreadPool t1;
static ThreadPool t2;
static ThreadPool t3;
static ThreadPool t4;
static ThreadPool t5;

public void run()
{
getRequest();
}

public static void main(String args[])
{
t1= new ThreadPool();
t2=new ThreadPool();
t1.start();
t2.start();
}

public static void getRequest()
{
while(ServerConnection.requestList.isEmpty())
{
try {
synchronized(t1){
t1.wait();
getSocket();
}
synchronized(t2){
t2.wait();
getSocket();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void getSocket()
{
if(!ServerConnection.requestList.isEmpty())
{
Socket s=ServerConnection.requestList.get(1);
System.out.println(s.toString());
}
}
}

Any help would be appreciated.

Thanks
Aditya
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Please use code tags. It will make your code much easier to read.

Your thread pool is running in a different application, and does not know the other application exists. They need to be running in the same JVM.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Too difficult for "beginning Java". Moving thread.
Adi Sharma
Ranch Hand

Joined: May 18, 2009
Posts: 33
Hey,

did you really move it to another thread??? If so, where?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Adi Sharma wrote:did you really move it to another thread??? If so, where?


If you are able to post in it, you found it...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java multithreading
 
Similar Threads
Null pointer exception probably due to synchronisation issues; problem in starting activity
Question on Threads
Producer/Consumer Design impacts exception handling
Chat At Server Part Both write and read at the same time?
Printing from threads in a particular sequence