-1

I want to write a code with two different threads. The first one does somethin, the second one waits a specific time. The thread that ends first should interrupt the other one. My problem is now, that the thread, I initialized first, cannot access/interrupt the second one, it always gives out the "symbol not found"-error. If I swap the positions of the threads in the code, it is the same, only the other way around. Is there a possibility, to make both threads "global" and accessable by the other one? Please give coding examples, where to put the public void main, the void run(), etc., if possible, so I just need to add the code itself. Thanks

Code examples:

    public class FTPUpload extends Thread {

      public static void main (String args[]) {
        _//some code_
        final Thread thread1 = new Thread(){;
          public void run() {
    _//code of thread1_
}  

final Thread thread2 = new Thread(){;
  public void run() {
   _//code of thread2_ 

}

thread1.start();
thread2.start();

       }
    }

4 Answers4

0

For your question is (currently?) a bit vague, my answer may not be that helpful. But...

Try declaring the Thread objects first and use them later. So each might be know to the other.

DerMike
  • 15,594
  • 13
  • 50
  • 63
0

You can create a bool static Variable which both can access and once one of them is finished sets it to true, and you have to check this variable during the job in each thread, either on different places or if you have a loop in the loop for example.

Alternatively you can write a dummy file somewhere by the thread finished 1st and keep checking if file exists in both threads. The main idea is having a shared resource both can access.

Read this Question it very informative: Are static variables shared between threads?

My Idea May not work up to some answers but the one with the file should actually work.

Community
  • 1
  • 1
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • 1
    No mention of volatile or synchronisation or locking which will be required if the second thread is to be sure to see the value... You obviously have not read page 33 of "Java Concurrency In Practice" [i do not have any affiliation with the book... But page 33 strongly affected me as a java developer and that book is awesome] – Stephen Connolly Sep 13 '12 at 08:10
  • Is it the one page with "Safe Publication"? – CloudyMarble Sep 13 '12 at 08:38
  • @Stephen Connolly: but i mentioned that the Idea with the static may not work including the explanation in the referenced Linked (did you open the Link??) – CloudyMarble Sep 13 '12 at 08:42
  • Mention volatile explicitly and/or explicitly mention synchronisation... Otherwise the answer is not good. – Stephen Connolly Sep 13 '12 at 08:51
  • it is the one with the thread and the variable and a thing being printed to stdout and you are asked to say what is it legal for a jvm implementation to print out... I don't want to give the answer as it is much more informative for others if they find out by looking at the code – Stephen Connolly Sep 13 '12 at 08:56
  • "Otherwise the answer is not good." ? good to knwo that ur here to decide which answers are good and which not. and please dont bother to answer. – CloudyMarble Sep 13 '12 at 09:21
  • I was just pointing out the reason for the down vote and the criteria for me switching it to an up vote... If you don't want to improve your answer then I will leave the down vote stand. Everyone is here to judge the quality of answers... That's what voting is for – Stephen Connolly Sep 13 '12 at 09:36
0

A typical solution for communicating between 2 threads is to use condition variables. Thread1 could block on the condition variable, then when Thread2 has done what it needs to do and wants to tell Thread1 to go, it signals Thread1 via the condition variable, thus releasing its block. Both threads must be initialized with the same condition variable. Here is an example.

If you want both threads to wait until the other is initialized, this can be performed using a barrier sync (called a CyclicBarrier in Java). If Thread1 hits the barrier sync first it will block, until the other thread hits the barrier sync. Once both have hit the barrier sync, then they will continue processing. Here is an example.

Both condition variables and barrier syncs are thread safe, so you dont have to worry about if you need to synchronize them or not.

Community
  • 1
  • 1
Brady
  • 10,207
  • 2
  • 20
  • 59
0

The general principal is to create a lock and condition outside both threads. The first thread acquires the lock and signals the condition when done. The second thread acquires the lock and awaits the condition (with timeout if needed). I am very concerned that you are relying on Thread.interrupt() which is a bad plan.

final Lock lock = new ReentrantLock();
final Condition done = lock.newCondition();
...
// in thread 1 when finished
lock.lock();
try {
  done.signalAll();
} finally {
  lock.unlock();
}
...
// in thread 2 for waiting
lock.lock();
try {
  done.await(30,TimeUnit.SECONDS); // wait for the done or give up waiting after 30s
} finally {
  lock.unlock();
}

Using the lock will ensure that both threads see a consistent view of shared objects, whereas Thread.interrupt() does not guarantee you have passed a boundary

A refinement is to use a CountDownLatch

final CountDownLatch latch = new CountDownLatch(1);
...
// in thread 1
latch.countDown();
...
// in thread 2
latch.await(30,TimeUnit.SECONDS)

This abstracts away the lock.

Others have suggested effectively a spin lock scanning for a file on the file system. Such an approach could lead to thread starvation or if not slower performance than a lock or latch based solution... Though for inter-process as opposed to inter-thread within the one jvm, file based is ok

I recommend the book "Java Concurrency In Practice" if you think you know threading go to a bookshop, open the book and try to predict what the program on page 33 will do... After reading that page you will end up buying the book

Stephen Connolly
  • 13,872
  • 6
  • 41
  • 63