473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

mulithread server with futureTask

oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1. class SocketServer extends JFrame{
  2.  
  3.        JLabel label = new JLabel("Text received over socket:");
  4.        JPanel panel;
  5.        JTextArea progress = new JTextArea();
  6.        ServerSocket server = null;
  7.  
  8.        Callable<String> reverseTask = new Callable<String>;
  9.  
  10.  
  11.           public void listenSocket(){
  12.             try{
  13.               server = new ServerSocket(4444); 
  14.             } catch (IOException e) {
  15.               System.out.println("Could not listen on port 4444");
  16.               System.exit(-1);
  17.             }
  18.             while(true){
  19.  
  20.                 ClientWorker w;
  21.                   try{
  22.                       w = new ClientWorker(server.accept(), progress);
  23.                         Thread t = new Thread(w);
  24.                         t.start();
  25.  
  26.               } catch (IOException e) {
  27.                 System.out.println("Accept failed: 4444");
  28.                 System.exit(-1);
  29.               }
  30.             }
  31.           }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.        SocketServer(){ //Begin Constructor
  41.            Font f = new Font("Dialog", Font.PLAIN, 16);
  42.  
  43.             progress.setFont(f);
  44.             JPanel p = new JPanel();
  45.             JButton b = new JButton("Start");
  46.             b.addActionListener(new ActionListener() {
  47.               public void actionPerformed(ActionEvent e) {
  48.                 ft = new FutureTaskCallback<String>(reverseTask);
  49.                 toReverse = progress.getText();
  50.                 exec.execute(ft);
  51.               }
  52.             });
  53.             p.add(b);
  54.             b = new JButton("Stop");
  55.             b.addActionListener(new ActionListener() {
  56.               public void actionPerformed(ActionEvent e) {
  57.                 if (ft != null) ft.cancel(true);
  58.               }
  59.             });
  60.             p.add(b);
  61.             Container cp = getContentPane();
  62.             cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
  63.  
  64.             add(p);
  65.             add(progress);
  66.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.             pack();
  68.             setVisible(true);
  69.        } //End Constructor
  70.  
  71.  
  72.  
  73.  
  74.           ExecutorService exec = Executors.newSingleThreadExecutor();
  75.               FutureTaskCallback<String> ft;          
  76.  
  77.  
  78.               String toReverse;
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.       protected void finalize(){
  92. //    Objects created in run method are finalized when 
  93. //    program terminates and thread exits
  94.          try{
  95.             server.close();
  96.         } catch (IOException e) {
  97.             System.out.println("Could not close socket");
  98.             System.exit(-1);
  99.         }
  100.       }
  101.  
  102.       public static void main(String[] args){
  103.           SocketServer frame = new SocketServer();
  104.         frame.setTitle("Server Program");
  105.             WindowListener l = new WindowAdapter() {
  106.                     public void windowClosing(WindowEvent e) {
  107.                             System.exit(0);
  108.                     }
  109.             };
  110.             frame.addWindowListener(l);
  111.             frame.pack();
  112.             frame.setVisible(true);
  113.             frame.listenSocket();
  114.       }
  115.     }
  116.  
why eclipse higlights this >>>. Callable<String> reverseTask = new Callable<String>;as an error

please help
Apr 25 '07 #1
6 3040
JosAH
11,448 Expert 8TB
why eclipse higlights this >>>. Callable<String> reverseTask = new Callable<String>;as an error

please help

Because Callable is an interface, you can't instantiate it. I'm sure Eclipse tells
quite a bit more than just flagging it as an error; it also gives you the reason
why it is an error (which most of the time is the way to a solution).

kind regards,

Jos
Apr 25 '07 #2
oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1.  while(true){
  2.           ClientWorker w;
  3.           FutureTask task = new FutureTask (new MyCallable ());
  4.           ExecutorService es = Executors.newSingleThreadExecutor ();
  5.           es.submit (task);
  6.           try{
  7.  
  8.               int result = task.get();
  9.               System.out.println ("Result from task.get () = " + result);
  10.  
  11.  
  12.             w = new ClientWorker(server.accept(),  output);
  13.             Thread t = new Thread(w);
  14.             t.start();
  15.           } catch (IOException e) {
  16.             System.out.println("Accept failed: 4444");
  17.             System.exit(-1);
  18.           }
  19.           es.shutdown (); 
  20.         }
  21.       }
  22.  
now eclipse says cannot convert form object to int
here>>> int result = task.get();
Apr 25 '07 #3
JosAH
11,448 Expert 8TB
now eclipse says cannot convert form object to int
here>>> int result = task.get();
That's because FutureTask is a generic class, i.e. you have to specify the type
of what shall be returned by the FutureTask. You didn't specify anything so the
return type is Object. An Object can not be cast to an int as Eclipse correctly
noticed.

kind regards,

Jos
Apr 25 '07 #4
oll3i
679 512MB
Expand|Select|Wrap|Line Numbers
  1. public class MyCallable implements Callable
  2. {String toReverse;
  3. JTextArea output = new JTextArea();
  4. ServerSocket server = null;
  5.   public String call () throws java.io.IOException, InterruptedException {
  6.  
  7.       ClientWorker w = new ClientWorker(server.accept(),  output);
  8.     Thread t = new Thread(w);
  9.      t.start();
  10.       ////Thread t = Thread.currentThread();
  11.       if (toReverse == null || toReverse.trim().equals(""))
  12.         throw new IllegalArgumentException("Set string to reverse");
  13.       if (t.isInterrupted()) return null;
  14.       char[] org = toReverse.toCharArray();
  15.       StringBuffer out = new StringBuffer();
  16.       if (t.isInterrupted()) return null;
  17.       for (int i = org.length-1; i>=0; i--) {
  18.         Thread.sleep(500);
  19.         out.append(org[i]);
  20.         if (t.isInterrupted()) return null;
  21.         output.setText(out.toString());
  22.         if (t.isInterrupted()) return null;
  23.       }
  24.       return out.toString();
  25.     }
  26.  
now i get java.lang.OutOfMemoryError: unable to create new native thread when i try to run a client i think it's in MyCallable
Apr 25 '07 #5
oll3i
679 512MB
help me please
Apr 25 '07 #6
oll3i
679 512MB
[code]
public void listenSocket(){

server = mycallable.getServer();
mycallable.setToReverse(textField.getText());
while(true){
///ClientWorker w;

FutureTaskCallback task = new FutureTaskCallback (mycallable);
ExecutorService es = Executors.newSingleThreadExecutor ();
es.submit (task);

es.shutdown ();
}
}[code]

this code causes Outofmemory error
Apr 25 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
14
by: Developer | last post by:
Hello All, i have recently installed VS2005 and was trying to install SQL sever 2000. I have Win XP' SP2. But when I tried installing, it only installed client tools and not the database. Can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.