What are threads?
|
|
WHY Threads?
|
|
About Threads in Java
Java threads are units of a Java computation executing Java methods. Java Virtual Machines support concurrent execution of Java threads. Such multithreading is pre-emptive and prioritised. Scheduling is regulated by priorities:
Java scheduler
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine (the scheduler) continues to execute threads until either of the following occurs:
Some but not all Java schedulers support time-slicing
Otherwise yield() method
|
|
Threads can be created by
|
|
Main methods of Java threads are:
Three useful inherited methods from Object class are
|
|
Java Thread Synchronisation
Thread executes until it
Higher priority thread other than running thread can become ready if
|
|
2 Alternatives to create a Thread
1) class extends Threadclass SimpleThread extends Thread {
}
class TestThread {
}
2) class implements Runnable, and then create instance of it and pass this to the general Thread constructor and start this thread.class SecondMethod implements Runnable {
}
class TestThread {
}
|
|
Testing for Time-slicing & Parallelismclass InfinityThread extends Thread {
}
class TestThread {
}
Output
|
|
Debugging ThreadsSome useful methods in Thread for debugging
|