Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads within threads in Java?

I am currently thinking about how to design a multithreading system in Java that needs to do some heavy network processing and database storage. The program will launch three basic threads at first. Along these basic threads, I would like to launch other threads not from the main program but from two of the threads. Is it possible for a thread to launch another thread leading to some sort of a hierarchy like:

> Parent ->t0 thread1 -> t1 tread1.1  
>        ->t0 thread2
>        ->t0 thread3 -> t2 thread3.1

t0= inital time
t1,t2 = time at a point in the running thread
t1 != t2 

If not could somebody provide a theoretical solution with references?

like image 885
user253530 Avatar asked Aug 28 '11 23:08

user253530


People also ask

Can you thread within a thread?

Yes a thread can launch another thread, and that thread can launch thread(s) and on and on... In the run() method of a thread - you can create and start other threads. JJ. JJ.

Can a thread create another thread in Java?

Java's multithreading system is built upon the Thread class, its methods, and its companion interface, Runnable. To create a new thread, your program will either extend Thread or implement the Runnable interface.

Can we run two threads simultaneously in Java?

Multi-thread programming allows us to run threads concurrently, and each thread can handle different tasks. Thus, it makes optimal use of the resources, particularly when our computer has a multiple multi-core CPU or multiple CPUs.

Can threads interact with each other?

There are three ways for threads to communicate with each other. The first is through commonly shared data. All the threads in the same program share the same memory space. If an object is accessible to various threads then these threads share access to that object's data member and thus communicate each other.


1 Answers

Yes a thread can launch another thread, and that thread can launch thread(s) and on and on...

In the run() method of a thread - you can create and start other threads.

like image 192
JJ. Avatar answered Sep 24 '22 23:09

JJ.