Results 1 to 15 of 15
- 04-10-2012, 03:36 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
how to run the two threads sequentially
hi all,
I have two threads by name T1, T2. T1 thread will generate the 10 multiplication table. T2 will generate the 20 multiplication table. my problem is i would like to control over the threads. I just want to run the T1 first and and then T2. Please find the following code which i have written. Any help would be great help for me. Thanks in Adavance
public class ThreadTestFire extends Thread{
/**
* @param args
*/
public ThreadTestFire(Runnable r){
super(r);
}
public static void main(String[] args) throws InterruptedException{
// TODO Auto-generated method stub
synchronized (ThreadTestFire.class) {
new ThreadTestFire(new MyThread(10)).start();
//Thread.currentThread().notifyAll();
}
synchronized (ThreadTestFire.class) {
new ThreadTestFire(new MyThread(20)).start();
}
synchronized (ThreadTestFire.class) {
new ThreadTestFire(new MyThread(30)).start();
}
//Thread.currentThread().join();
}
}
MyThread Class
*****************************
public class MyThread implements Runnable {
private int x;
public MyThread(int x){
this.x = x;
}
@Override
public void run() {
int y = 0;
for(int i = 1; i <=10; i++){
y = this.x * i;
System.out.println( i +" * " + this.x + "=" + y);
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
*********************************************
regards & thanks,
Nirmala Vijaya Sekhar Varre
- 04-10-2012, 03:55 PM #2
Re: how to run the two threads sequentially
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-10-2012, 04:13 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: how to run the two threads sequentially
Hi,
Thank you for the response. But i just want to have the control over the thread. I just want to run the thread as i like not as JVM/OS like.
I just want to understand the concept of Synchronization.
regards & thanks,
Nirmala Vijaya Sekhar Varre
- 04-10-2012, 04:17 PM #4
Re: how to run the two threads sequentially
If you want to run them sequentially, you probably shouldn't be using threads. There are other ways around it, but it really doesn't make sense to use threads if you always want to do things sequentially.
Recommended reading: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-10-2012, 06:36 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: how to run the two threads sequentially
hi,
i got it what you are saying but i just want catch hold of a thread one thread and i want to have control over the thread which are currently running in my app. if you have any idea please do share with me.
regards
~vijay
- 04-10-2012, 06:47 PM #6
Re: how to run the two threads sequentially
Did you read the tutorial I gave you? It contains plenty of information, including how to accomplish what you're asking about.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
-
Re: how to run the two threads sequentially
Consider using an Executor obtained via Executors.newSingleThreadScheduledExecutor(). The API states:
Creates a single-threaded executor that can schedule commands to run after a given delay, or to execute periodically. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newScheduledThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.
- 04-10-2012, 07:10 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: how to run the two threads sequentially
KevinWorkman,
i am pretty new this forum..so please provide me link to your tutorial which you have mentioned about in ur previous response.
regards,
~vijay
-
Re: how to run the two threads sequentially
- 04-11-2012, 08:14 PM #10
Member
- Join Date
- Mar 2012
- Location
- Novosibirsk, Russia
- Posts
- 15
- Rep Power
- 0
Re: how to run the two threads sequentially
Threads are controlled in 2 ways:
1) programmer feeds threads with resources and input information
2) JVM/OS makes best to run them in parallel, as long as resources/information are available
The concept of synchronization is to provide correct way to pass resources/information to threads: the act of passing takes some time, and during that time only one thread have access to the resources being passed.
Since your threads do not exchange information, there is no room for synchronization. Trying to control threads in this situation means building a thread framework upon existing JVM/OS implementation. This is an interesting task, but to implement it, you should clearly understand in what way you are not satisfied with existing implementation. As far as I can see, this task is not what you should do just now.
- 04-12-2012, 01:10 AM #11
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Re: how to run the two threads sequentially
Why don't you just stick a speep() command between each thread start? This will start each thread slightly after the last one, so all threads will fire x many milliseconds after the last one is done...
Java Code:public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub synchronized (ThreadTestFire.class) { new ThreadTestFire(new MyThread(10)).start(); //Thread.currentThread().notifyAll(); } sleep (5); synchronized (ThreadTestFire.class) { new ThreadTestFire(new MyThread(20)).start(); } sleep (5); synchronized (ThreadTestFire.class) { new ThreadTestFire(new MyThread(30)).start(); } //Thread.currentThread().join(); }
- 04-12-2012, 03:45 PM #12
Re: how to run the two threads sequentially
Are you sure about that? Only the main thread will access that method, so I'm not sure what those synchronized blocks are for. And you are delaying the start of each thread, but not their completion. For example, if each thread was going to perform an action that took somewhere between an hour and a day, you could not guarantee that the first thread goes first, then the second one, then the third one, which I believe is what the OP wants.
The answer is in the tutorial I linked to. But really, the answer is more a question- why would you bother using threads if you're just going to keep them sequential?How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
-
Re: how to run the two threads sequentially
Kevin, I think that there are applications for this sort of thing, for instance if you want to use a queue to hold Runnables or Futures and allow them to be processed sequentially in a background thread.
- 04-13-2012, 08:26 AM #14
Re: how to run the two threads sequentially
Moved from Advanced Java for better topic alignment.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-13-2012, 08:00 PM #15
Re: how to run the two threads sequentially
Oh yeah, I absolutely agree with that. There are definitely times when you want to control thread execution, which is why I linked to the concurrency tutorial. But for this case, I'm not sure the OP should be using threads at all. I would predict that he's misunderstanding an assignment (or maybe the assignment is a purposely bad use of threading), but that's just a guess. Either way, the "how" to do this is covered in the tutorial, regardless of the "why", which is the part I'm curious about.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
Similar Threads
-
Threads per Connection or Threads per Request
By Felic in forum New To JavaReplies: 4Last Post: 11-22-2011, 10:15 PM -
Threads!!!
By smartino in forum New To JavaReplies: 67Last Post: 11-13-2011, 06:24 PM -
The right way of using threads
By N00Bie in forum New To JavaReplies: 9Last Post: 02-20-2011, 05:39 PM -
GUI and Threads
By rp181 in forum Threads and SynchronizationReplies: 1Last Post: 10-10-2009, 09:39 PM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 11:25 AM
Bookmarks