• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java multithreading

 
Adi Sharma
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for "beginning Java". Moving thread.
 
Adi Sharma
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

did you really move it to another thread??? If so, where?
 
Henry Wong
author
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic