In my app I start multiple media downloads in Threads via ThreadPoolExecutor. Now I want to be able to pause particular download threads. How I can do this?
well I'm not sure how you have implemented your code, so I'm just guessing here.
One way of doing this, by keeping track of your Threads,
for example create a Map :
Map<String,Thread> threads=new HashMap<String,Thread>();// ensure each Thread has a unique id, in this case its supposedly a String. then you can control them from outside your thread pool.
here is a hacked implementation:
public class hello{
public static void main(String...strings )throws Exception{
ExecutorService executor = Executors.newFixedThreadPool(5);
Map<String,Thread> threads=new HashMap<String, Thread>();
for(int i=0;i<5;i++){
Thread t = new myRunnable((i+1) +" ");
threads.put((i+1)+"", t);
executor.execute(t);
}
Thread.sleep(2000);
((myRunnable)threads.get("1")).isSuspened=true;
}
private static class myRunnable extends Thread{
String a;
public boolean isSuspened=false;
public myRunnable(String a) {
this.a=a;
// TODO Auto-generated constructor stub
}
#Override
public void run(){
while(true){
if(isSuspened){
continue;
}
try{
System.out.println(a);
Thread.sleep(2000);
}catch(Exception e){}
}
}
}
}
Related
I'm implementing a program which contains different tasks and all have implemented Runnable. e.g. there is a task which works on a database and sends some of the tuples to a synchronized shared memory and subsequently, there is another thread which checks the shared memory and sends messages to a queue. Moreover, these two threads iterate over an infinite while loop.
Already, I have used the fixedThreadPool to execute these threads.
The problem is that sometimes program control remained in the first running thread and the second one never gets the chance to go to its running state.
Here is a similar sample code to mine:
public class A implements Runnable {
#Override
public void run() {
while(true) {
//do something
}
}
}
public class B implements Runnable {
#Override
public void run() {
while(true) {
//do something
}
}
}
public class Driver {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
A a = new A();
executorService.execute(a);
B b = new B();
executorService.execute(b);
}
}
I'd also done something tricky, make the first thread to sleep once for a second after a short period of running. As a result, it makes the second thread to find the chance for running. But is there any well-formed solution to this problem? where is the problem in your opinion?
This is a good example of Producer/Consumer pattern. There are many ways of implementing this. Here's one naive implementation using wait/notify pattern.
public class A implements Runnable {
private Queue<Integer> queue;
private int maxSize;
public A(Queue<Integer> queue, int maxSize) {
super();
this.queue = queue;
this.maxSize = maxSize;
}
#Override
public void run() {
while (true) {
synchronized (queue) {
while (queue.size() == maxSize) {
try {
System.out.println("Queue is full, " + "Producer thread waiting for "
+ "consumer to take something from queue");
queue.wait();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Random random = new Random();
int i = random.nextInt();
System.out.println("Producing value : " + i);
queue.add(i);
queue.notifyAll();
}
}
}
}
public class B implements Runnable {
private Queue<Integer> queue;
public B(Queue<Integer> queue) {
super();
this.queue = queue;
}
#Override
public void run() {
while (true) {
synchronized (queue) {
while (queue.isEmpty()) {
System.out.println("Queue is empty," + "Consumer thread is waiting"
+ " for producer thread to put something in queue");
try {
queue.wait();
} catch (Exception ex) {
ex.printStackTrace();
}
}
System.out.println("Consuming value : " + queue.remove());
queue.notifyAll();
}
}
}
}
And here's hot we set things up.
public class ProducerConsumerTest {
public static void main(String[] args) {
Queue<Integer> buffer = new LinkedList<>();
int maxSize = 10;
Thread producer = new Thread(new A(buffer, maxSize));
Thread consumer = new Thread(new B(buffer));
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(producer);
executorService.submit(consumer);
}
}
In this case the Queue acts as the shared memory. You may substitute it with any other data structure that suits your needs. The trick here is that you have to coordinate between threads carefully. That's what your implementation above lacks.
I know it may sound radical, but non-framework parts of asynchonous code base should try avoiding while(true) hand-coded loops and instead model it as a (potentially self-rescheduling) callback into an executor
This allows more fair resources utilization and most importantly per-iteration monitoring instrumentation.
When the code is not latency critical (or just while prototyping) the easiest way is to do it with Executors and possibly CompletableFutures.
class Participant implements Runnable {
final Executor context;
#Override
public void run() {
final Item work = workSource.next();
if (workSource.hasNext()) {
context.execute(this::run);
}
}
}
I am trying to execute two method where two methods will execute one by one after some interval, and I am using ExecuterService. I have implemented some portion of the code but the full functionality I could not achieve till now, here I am posting my code
public class ExampleExecuterService {
private static final int MYTHREADS = 3000;
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
Object methodList[]={
aMethod(),bMethod()
};
for(int i=0;i<methodList.length;i++){
Object myList = methodList[i];
Runnable worker = new MyRunnable(myList);
executor.execute(worker);
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("\nFinished all threads");
}
public static class MyRunnable implements Runnable {
private Object myList=null;
MyRunnable(Object myList) {
this.myList = myList;
}
#Override
public void run() {
try{
myList.wait(2000);
}catch(Exception e){
e.printStackTrace();
}
}
}
private static Object bMethod() {
System.out.println("This is inside method a ");
return null;
}
private static Object aMethod() {
System.out.println("This is inside method b ");
return null;
}
}
I want aMethod() and bMethod() that should run 20 seconds after and in the end the executer will stop. How to do that with my code. Somebody please help me.
Object methodList[]={
aMethod(),bMethod()
};
This is not a list of your methods. This is a list of what your methods return (=null).
In Java, methods are not objects. If you want to store methods in a list or array, you have to wrap them inside objects. The usual way of doing this is by using the Runnable interface or something of the kind.
In your case, it could look like this:
Runnable[] methods = new Runnable[]{
new Runnable(){ // Object wrapper for method a
#Override
public void run(){ // method a
System.out.println("This is inside method a");
}
},
new Runnable(){ // Object wrapper for waiting
#Override
public void run(){ // method to wait for 20s
try{ Thread.sleep(20000); }catch(Exception e){}
}
},
new Runnable(){ // Object wrapper for method b
#Override
public void run(){ // method b
System.out.println("This is inside method b");
}
}
};
After that you can submit this array of "methods" to your executor service:
ExecutorService service = Executors.newSingleThreadExecutor();
for(Runnable r : methods)
service.submit(r);
service.shutdown();
However, keep in mind that ExecutorService is primarily meant to execute tasks concurrently (= in parallel), while you want to execute them sequentially.
In you know that you'll always need a sequential, single-thread behaviour, you should drop ExecutorService and simply:
for(Runnable r : methods)
r.run();
EDIT: Full main method
public static void main(String[] args) throws Exception {
Runnable[] methods = new Runnable[]{
new Runnable(){ // Object wrapper for method a
#Override
public void run(){ // method a
System.out.println("This is inside method a");
}
},
new Runnable(){ // Object wrapper for waiting
#Override
public void run(){ // method to wait for 20s
try{ Thread.sleep(20000); }catch(Exception e){}
}
},
new Runnable(){ // Object wrapper for method b
#Override
public void run(){ // method b
System.out.println("This is inside method b");
}
}
};
ExecutorService service = Executors.newSingleThreadExecutor();
for(Runnable r : methods)
service.submit(r);
service.shutdown();
// Wait until all threads are finish
while (!service.isTerminated()) {}
System.out.println("\nFinished all threads");
}
I didn't find a better solution for the waiting 20 seconds in between, but how about this:
ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(task1);
service.submit(() -> {
Thread.sleep(20000);
return null;
});
service.submit(task2);
I have a gatherer, that searches for moves in a game. I search in a recursive search, to get every possible move from the game.
For performance cause, I use a Threadpool and every found move adds a new Thread to the pool, to maybe extend the old move.
Here is some code:
protected static List<Runnable> threads;
private static ExecutorService threadPool;
protected final synchronized void hookThread(Runnable thread) {
if (threadPool == null) {
threadPool = Executors.newFixedThreadPool(15);
threads = new ArrayList<Runnable>();
}
threadPool.execute(thread);
threads.add(thread);
}
protected abstract class GathererRunnable implements Runnable {
#Override
public final void run() {
onRun();
threads.remove(this);
}
public abstract void onRun();
}
This is a snippet of the parent class. Now comes the child, that searches for the moves.
private void extendMove(final byte[] stones, final ByteLayMove move) {
Runnable r = new GathererRunnable() {
#Override
public void onRun() {
// fancy search stuff
if (moveIsFound)
extendMove(...);
}
};
hookThread(r);
}
The problem is now, that I don't know how I should can wait for the threads to finish.
I tried to use a int, that counts up on Thread Creation and down on Thread Completion, but that also resultet in a too early search abortion.
Do you have an idea if there is a nice way to wait for these threads?
I already thought about a BlockingQueue, but I don't have any idea how to implement it properly.
Greeting Kevin
Below program has implemented producer consumer scenario using BlockingQueue , you can use such approach while writing your own implementation.
import java.util.concurrent.*;
public class ThreadingExample {
public static void main(String args[]){
BlockingQueue<Message> blockingQueue = new ArrayBlockingQueue<Message>(100);
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new Producer(blockingQueue));
exec.execute(new Consumer(blockingQueue));
}
}
class Message{
private static int count=0;
int messageId;
Message(){
this.messageId=count++;
System.out.print("message Id"+messageId+" Created ");
}
}
class Producer implements Runnable{
private BlockingQueue<Message> blockingQueue;
Producer(BlockingQueue<Message> blockingQueue){
this.blockingQueue=blockingQueue;
}
#Override
public void run(){
while(!Thread.interrupted()){
System.out.print("Producer Started");
try {
blockingQueue.put(new Message());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Producer Done");
}
}
}
class Consumer implements Runnable{
private BlockingQueue<Message> blockingQueue;
Consumer(BlockingQueue<Message> blockingQueue){
this.blockingQueue=blockingQueue;
}
#Override
public void run(){
while(!Thread.interrupted()){
System.out.print("Concumer Started");
try{
Message message = blockingQueue.take();
System.out.print("message Id"+message.messageId+" Consumed ");
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Concumer Done");
}
}
}
i need help, i try to use synchronization thread in java but it can't run...
i have two class with thread like this
for(int j=0;j<idW.length;j++){
webtext = d.getWebText(idW[j]);
ThreadPrepo tpo =new ThreadPrepo(webtext, host[j%jumhost], "server", 1099,idW[j]);
Thread t1=new Thread(tpo);
t1.start();
}
//thread untuk setfitur tanpa rmi
int ukuran=idW.length;
ProsesSetfitur pro=new ProsesSetfitur(idW);
Thread t2=new Thread(pro);
t2.start();
this is the code in class threadprepo :
public class ThreadPrepo implements Runnable{
String host,server,c,webtext;
int port,idweb;
DataDB db=new DataDB();
public ThreadPrepo(String webtext,String host,String server,int port,int idweb){
this.webtext=webtext;
this.host=host;
this.server=server;
this.port=port;
this.idweb=idweb;
}
#Override
public void run(){
preponi();
}
public synchronized void preponi(){
try{
System.out.println("hostnya :"+host);
Registry reg=LocateRegistry.getRegistry(host,port);
Sportrmijob rmi=(Sportrmijob) reg.lookup("server");
rmi.SetInput(webtext);
List l=rmi.getresult();
String[] hasilprep=new String[l.size()];
for(int k=0;k<l.size();k++){
hasilprep[k]=l.get(k).toString();
}
db.insertWordney(idweb, hasilprep);
String [][] frekdb=db.getFrekDB(idweb);
db.doinsertfrek(idweb,frekdb);
}
catch(Exception e){
System.out.println("error di class threadprepo "+e.getMessage());
}
}
}
and then this is code in class prosesSetFitur
public class ProsesSetfitur implements Runnable{
DataDB d=new DataDB();
int []idweb;
public ProsesSetfitur(int[]idweb){
this.idweb=idweb;
}
#Override
public void run(){
try{
Thread.sleep(500);
setfitur();
}
catch(Exception e){
System.out.println("error setfitur "+e.getMessage());
}
}
public synchronized void setfitur() throws InterruptedException{
System.out.println("(proses setfitur)");
String []allkata;
String fitur;
String []fiturs=new String[15];
String []kata_kata=new String[15];
System.out.println("nilai iD="+idweb.length);
for(int s=0;s<idweb.length;s++){
//System.out.println("IDWEEEEEEEEEEB"+idweb[s]);
allkata=d.getUrutanKata(idweb[s]);
for(int u=0;u<15;u++){
// System.out.println("PERULANGAN U KE"+u);
if(u<=4){
fitur="T";
//System.out.println("kata ke" +u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
else if(u>4&&u<10){
fitur="S";
//System.out.println("kata ke"+u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
else if(u>=10&&u<15){
fitur="R" ;
//System.out.println("kata ke"+u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
}
d.insertfitur(idweb[s], kata_kata, fiturs);
}
}
can anyone give me solution to solve this problem...why thread in class ProsesSetFitur is execute first?how synchronization thread can work?please help...
public void run(){
try{
Thread.sleep(500);
setfitur();
}
run() method called only once when you start a thread. Again for different thread run() method will be different and call only once for the thread.
Also your preponi() and setfitur() called single time from run(). thats why you should not put synchronized modifier before preponi() and setfitur().
you should use synchronized when multiple thread access same resource or same function or same code block to make it thread safe.
Thread []tArray=new Thread[idW.length];
for(int j=0;j<idW.length;j++)
{
webtext = d.getWebText(idW[j]);
ThreadPrepo tpo =new ThreadPrepo(webtext, host[j%jumhost], "server", 1099,idW[j]);
tArray[j]=new Thread(tpo);
tArray[j].start();
tArray[j].join();
}
//thread untuk setfitur tanpa rmi
int ukuran=idW.length;
ProsesSetfitur pro=new ProsesSetfitur(idW);
Thread t2=new Thread(pro);
t2.start();
A thread sleep may be a pragmatic solution but it is not a guarantee for thread synchronization. To coordinate thread actions you should go for the basic wait/notify pattern whereas the threads uses conditions to perform certain actions. For a good introduction read this articles.
http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html
http://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example
I made a producer-consumer program. It's just a program in core java without any GUI(Swing or SWT). It has one producer who put objects into the queue.
Also there is a few consumers who must add some staff(for example String) into Every object in that shared queue. So, every consumer must handle every object in a shared queue.
In this case - every BookShelf must have items from All consumers in "books" ArrayList. consumers.
Question: What condition should I use in consumers to finish their threads correctly?
Here are the code fragments of the program. Maybe I implemented it in wrong way.
Here is an object for the queue:
public class BookShelf {
private int id;
private String name;
private int height;
private int weigh;
List<String> books = Collections.synchronizedList(new ArrayList<String>());
public BookShelf(int id, String name) {
this.id = id;
this.name = name;
}
public void addBook(String book) {
books.add(book);
}
public boolean eq(String book) {
synchronized (books) {
for (String b: books) {
if (b.equalsIgnoreCase(book)) {
return true;
}
}
}
return false;
}
other setters and getters..
}
Here is the producer class:
public class Producer implements Runnable {
private BlockingQueue myQueue;
public Producer(BlockingQueue myQueue) {
this.myQueue = myQueue;
}
public void run() {
for(int i=0; i<7; i++){
try {
System.out.println("Produced: " + i);
BookShelf myBookShelf = new BookShelf(i, "book #" + i);
myQueue.put(myBookShelf);
} catch (InterruptedException ex) {
//Proper handle
}
}
}
}
Here is one of consumers class:
public class Consumer implements Runnable {
private BlockingQueue myQueue;
public Consumer(BlockingQueue myQueue) {
this.myQueue = myQueue; }
public void run() {
while(true){
try {
BookShelf tempBookShelf = (BookShelf) myQueue.take();
//eq() is my method to check if ArraList has a book.
if (tempBookShelf.eq("Abc book")) {
System.out.println("It already has book");
myQueue.put(tempBookShelf);
Thread.sleep(2000);
} else {
tempBookShelf.addBook("Abc book");
myQueue.put(tempBookShelf);
Thread.sleep(2000);
}
} catch (InterruptedException ex) {
//Proper handle
}
}
}
}
Here is main class:
public class ProducerConsumerTest {
public static void main(String[] args) {
BlockingQueue sharedQueue = new LinkedBlockingQueue();
Thread prodThread = new Thread(new Producer(sharedQueue));
Thread consThread = new Thread(new Consumer(sharedQueue));
Thread consThread2 = new Thread(new Consumer2(sharedQueue));
prodThread.start();
consThread.start();
consThread2.start();
}
}
Register each consumer with the producer. Each consumer has its own queue and the producer puts the object into all the queues. Each consumer then process on the same instance of the object.
public interface Consumer{
public void process(BookShelf bs);
}
public class Producer implements Runnable{
private final List<Consumer> consumers = new CopyOnWriteArrayList<Consumer>(); // thread safe but not efficient with lots of changes
public void register(Consumer c){
consumers.add(c); // thread safe
}
public void run(){
for(;;){
BookShelf bs = generateBookShelfByWhateverMeans();
for (Consumer c : consumers){
c.process(bs);
}
}
}
}
public class BookShelfConsumer implements Runnable, Consumer{
private final BlockingQueue<BookShelf> queue = new LinkedTransferQueue<BookShelf>(); // unbounded & thread safe
public void process(BookShelf bs){
queue.offer(bs); // non-blocking
}
public void run(){
for(;;){
BookShelf bs = queue.take(); // blocks until got object or interrupted
// catch InterruptedException
// do whatever this consumer is supposed to do with the object
}
}
}
I would try using SwingWorker instead. It has a done() method that is executed when it's finished. See this page for some code examples.
If it's not Swing you are using, there is a similar function in Swt called Jobs. Check this page for examples. It also has a done() method being executed when the job is done.
Also there is a few(N number) consumers who must add some staff(for example String) into Every object in that shared queue
I assume you mean every consumer must add their thing to every object which ever enters the queue. In that case, this is not a producer-consumer problem, this is more like an observer-observable problem. Basically, when a new BookShelf is created, that is the Observable. All of the Observers should be notified about the BookShelf and given the opportunity to add their own Book.
I recommend using a ConcurrentLinkedQueue in Bookshelf instead of a synchronized list - it's lock free (doesn't need to be synchronized) and will probably be more efficient.
To end your consumers, change their while(true) loops to while(!cancel) loops. Give each consumer a cancel boolean as an instance variable that initializes to false, and give them a cancel() method that sets cancel to true. Call cancel() on your consumers when you're done with them. If you will always be canceling all of your consumers at once (instead of selectively canceling some but not others), then you can use a static cancel instead of an instance cancel.