Create Thread with Thread Class
public class MyThread extends Thread{
MyThread(int ID){this.ID = ID;}
public void run(){
for (int i = 0; i<100; i++) System.out.println(ID + "\t" + i);
}
private int ID;
}
public class Main {
public static void main(String[] args) {
MyThread[] threads = new MyThread[4];
for (int i = 0; i<4; i++) threads[i] = new MyThread(i);
for (MyThread t : threads) t.start();
}
}
Create Thread with Runnable Interface
public class MyThread implements Runnable{
MyThread(int ID){this.ID = ID;}
public void run(){
for (int i = 0; i<100; i++) System.out.println(ID + "\t" + i);
}
private int ID;
}
public class Main {
public static void main(String[] args) {
Thread[] threads = new Thread[4];
for (int i = 0; i<4; i++) threads[i] = new Thread(new MyThread(i));
for (Thread t : threads) t.start();
// Or in other words
MyThread MY = new MyThread(6);
Thread example = new Thread(MY);
example.start();
}
}
Thread States
- https://uncodemy.com/technology/lifecycle-and-states-of-a-thread-in-java/
- https://crunchify.com/java-thread-state-introduction-with-example/
Thread priority
class ThreadDemo extends Thread {
public void run(){
System.out.println("Inside run method");
}
public static void print_priorities(ThreadDemo[] threads){
for (int i = 0; i<threads.length; i++)
System.out.println(threads[i].getPriority());
System.out.println();
}
public static void set_priorities(ThreadDemo[] threads,int[] priorities){
for (int i = 0; i<threads.length; i++)
threads[i].setPriority(priorities[i]);
}
public static void main(String[] args){
ThreadDemo[] threads = new ThreadDemo[3];
for (int i = 0; i<3; i++) threads[i] = new ThreadDemo();
print_priorities(threads);
set_priorities(threads,new int[]{2,5,8});
print_priorities(threads);
//for main thread
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getPriority());
}
}
5
5
5
2
5
8
main
5
Thread Class
- .setDaemon(true); Marks this thread as either a daemon thread or a user thread.
- .yield(); A hint to the scheduler that the current thread is willing to yield its current use of a processor.
https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/lang/Thread.html
What is a deamon thread
A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.
You can use the setDaemon(boolean)
method to change the Thread
daemon properties before the thread starts.
https://stackoverflow.com/questions/2213340/what-is-a-daemon-thread-in-java
Semaphore
https://preshing.com/20150316/semaphores-are-surprisingly-versatile/
Synchronize
class ThreadDemo extends Thread {
public void run(){
increment(100_000);
}
public static synchronized void increment(int count){
for (int i = 0; i<count; i++) myint++;
}
static int myint=0;
public static void main(String[] args){
ThreadDemo t1 = new ThreadDemo();
ThreadDemo t2 = new ThreadDemo();
t1.start();
t2.start();
try{
t1.join();
t2.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(myint); // 200000
}
}
Leave a Reply