Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

executor

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pool-1-thread-2
pool-1-thread-1

why doesn't program stop ? why does it hang after above output?

and another thing, i invoked execute method two times, does it mean it will start two new threads using only one executor object?
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program doesn't stop because the Executor is still running. Try e.shutdownNow() at the end of your code snippet.

When you called Executors.newFixedThreadPool(3) you created an executor with 3 threads (read the javadocs). When you call execute() it uses one of those 3 threads to execute the runnable.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuel Kadziela wrote:The program doesn't stop because the Executor is still running. Try e.shutdownNow() at the end of your code snippet.

When you called Executors.newFixedThreadPool(3) you created an executor with 3 threads (read the javadocs). When you call execute() it uses one of those 3 threads to execute the runnable.


Thanks.I am using Executor interface not ExecutorSerive interface.It hasn't any shutdown method.

ExecutorExample1.java:17: error: cannot find symbol
e.shutdownNow();
^
symbol: method shutdownNow()
location: variable e of type Executor
1 error
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you should probably switch to the ExecutorService interface.
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the javadocs

And just change this:


to this:

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i will use that.Seems Executor is useless, a car without break.
 
sajid toor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i know,thanks for sharing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic