Thread

 Process memory space = Heap

Each application has its own memory space, also known as the heap. 

The heap isn't shared between two applications or two processes, they each have their own.


A thread is a single unit of execution, within a process.

Each process can have multiple threads. Every application has at least one thread, and that is the main thread. 

Threads are fundamental building blocks, to support concurrency, in a java application. They are essential, because they allow us to perform multiple tasks simultaneously, within a single process. 




package org.example;

public class Main {
public static void main(String[] args) {
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getClass().getName());
System.out.println(currentThread);
printThreadState(currentThread);
currentThread.setName("MainGuy");
currentThread.setPriority(Thread.MAX_PRIORITY);
printThreadState(currentThread);
}

public static void printThreadState(Thread thread) {
System.out.println("--------------------------");
System.out.println("Thread ID: " + thread.getId());
System.out.println("Thread Name: " + thread.getName());
System.out.println("Thread Priority: " + thread.getPriority());
System.out.println("Thread State: " + thread.getState());
System.out.println("Thread Group: " + thread.getThreadGroup());
System.out.println("Thread is alive: " + thread.isAlive());
System.out.println("--------------------------");
}
}


--- start

package org.example;

public class CustomThread extends Thread {

@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.print(" 1 ");
try {
Thread.sleep(500); // adding a 1-second delay between each count
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


package org.example;

import java.util.concurrent.TimeUnit;

public class Main {
public static void main(String[] args) {
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getClass().getName());
System.out.println(currentThread);
printThreadState(currentThread);
currentThread.setName("MainGuy");
currentThread.setPriority(Thread.MAX_PRIORITY);
printThreadState(currentThread);
CustomThread customThread = new CustomThread();
customThread.start();
for (int i = 0; i < 3; i++) {
System.out.print(" 0 ");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void printThreadState(Thread thread) {
System.out.println("--------------------------");
System.out.println("Thread ID: " + thread.getId());
System.out.println("Thread Name: " + thread.getName());
System.out.println("Thread Priority: " + thread.getPriority());
System.out.println("Thread State: " + thread.getState());
System.out.println("Thread Group: " + thread.getThreadGroup());
System.out.println("Thread is alive: " + thread.isAlive());
System.out.println("--------------------------");
}
}


-- run

package org.example;

import java.util.concurrent.TimeUnit;

public class Main {
public static void main(String[] args) {
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getClass().getName());
System.out.println(currentThread);
printThreadState(currentThread);
currentThread.setName("MainGuy");
currentThread.setPriority(Thread.MAX_PRIORITY);
printThreadState(currentThread);
CustomThread customThread = new CustomThread();
customThread.run();
for (int i = 0; i < 3; i++) {
System.out.print(" 0 ");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void printThreadState(Thread thread) {
System.out.println("--------------------------");
System.out.println("Thread ID: " + thread.getId());
System.out.println("Thread Name: " + thread.getName());
System.out.println("Thread Priority: " + thread.getPriority());
System.out.println("Thread State: " + thread.getState());
System.out.println("Thread Group: " + thread.getThreadGroup());
System.out.println("Thread is alive: " + thread.isAlive());
System.out.println("--------------------------");
}
}









































Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class