Groups
Groups
Sign in
Groups
Groups
hackerdojo-javaconcurrency
Conversations
About
Send feedback
Help
Modified HW1a Solutions
0 views
Skip to first unread message
dc
unread,
Nov 15, 2010, 8:44:57 PM
11/15/10
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to hackerdojo-javaconcurrency
import java.io.Console;
import java.util.concurrent.*;
@net.jcip.annotations.ThreadSafe
public class StringLength {
public class ShowLength implements Runnable{
private final String str;
public ShowLength(String s){
this.str = s;
}
public void run(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Interrupted, was calculating length of <" +
str + ">");
Thread.currentThread().interrupt();
return;
}
System.out.println("Length of <" + str + "> is " + str.length());
}
}
/**
* @param args
*/
public static void main(String[] args) throws InterruptedException,
ExecutionException {
StringLength strLen = new StringLength();
Console c = System.console();
ExecutorService ex = Executors.newCachedThreadPool();
Future<?> f1=null;
System.out.println("Please type in a string:");
while(true){
String input = c.readLine();
System.out.println("Input: "+input);
System.out.println("Please type in another string: ");
if(input.equals("q")){
//System.exit(1); //immediate shutdown
ex.shutdownNow();
break;
}
ShowLength sl = strLen.new ShowLength(input);
ex.submit(sl);
f1 = ex.submit(sl);
}
f1.get();
}
}
Reply all
Reply to author
Forward
0 new messages