Java.Multithreading.How can a thread be started forcibly?

ScenarioCan you do it?How?
Start a thread without calling start()❌ Nostart() is required
Start a thread from another thread externally❌ NoCaller must control it
Force a paused/sleeping thread to resume❌ Not directlyYou can interrupt it
Force execution of run() logic manually✅ YesJust call run() (but runs in current thread)
Use thread pools to schedule execution✅ YesUse ExecutorService
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.How can a thread be started forcibly?

Java.Multithreading.What is the difference between start() and run() methods in Thread?

✅ The Thread class in Java just implements the Runnable interface. so…

🧵 The Core Difference

MethodWhat it Does
start()Starts a new thread of execution (calls run() on a new thread)
run()Just a normal method call, executes in the current thread
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is the difference between start() and run() methods in Thread?

Java.Multithreading.What is the difference between Thread and Runnable?

Let’s break it down step by step:


🧠 Core Difference

ConceptThreadRunnable
What is it?A class that represents a thread of executionA functional interface (task to run in a thread)
RoleExecutes the codeDefines the code to run
RelationshipThread is both a task and the runnerRunnable is just the task; needs a thread to run it
Flexibility❌ Less flexible (extends Thread)✅ More flexible (can be reused/shared)
UsageThread t = new MyThread(); t.start();Thread t = new Thread(new MyRunnable()); t.start();
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is the difference between Thread and Runnable?

Java.Multithreading.How can a thread be created?

🔨 1. Extending Thread Class

class MyThread extends Thread {
    public void run() {
        System.out.println("Hello from thread!");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.start(); // Don't call run() directly!
    }
}

✅ Easy to use
❌ Can’t extend another class (Java has single inheritance)

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.How can a thread be created?

Java.Multithreading.What are “green threads” and do they exist in Java?

🌱 What Are Green Threads?

Green threads are threads that are scheduled and managed entirely by the JVM (or user-level runtime), not the OS.

That means:

  • The JVM simulates threading using only one OS thread (or very few)
  • Thread switching is handled inside the JVM, not by the OS
  • Good for platforms where OS threads are expensive or unavailable

🧠 Think of green threads as “user-mode” threads.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What are “green threads” and do they exist in Java?

Java.Multithreading.What is the difference between a process and a thread?

🧠 What’s the Difference Between a Process and a Thread?

FeatureProcessThread
DefinitionA program in execution (independent)A lightweight unit of execution within a process
MemoryHas its own memory spaceShares memory with other threads in same process
IsolationFully isolated from othersNot isolated — shares heap & static data
CommunicationExpensive (IPC: sockets, pipes)Cheap (shared memory, synchronized access)
OverheadHeavyLight
Crash impactCrash of one process doesn’t affect othersCrash of one thread can affect whole process
SchedulingHandled by OSAlso scheduled by OS (as lightweight processes)
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is the difference between a process and a thread?

Java.Multithreading.What is ordering, as-if-serial semantics, sequential consistency, visibility, atomicity, happens-before, mutual exclusion, safe publication?

🧩 1. Ordering

Ordering is about the sequence of execution of operations (like reads and writes). It matters because compilers, JVM, and CPUs may reorder instructions to optimize performance.

x = 1;
y = 2;

You’d assume x is set before y, but the JVM might reorder this if it thinks it’s safe to do so — unless there’s a rule (like happens-before) that prevents it.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is ordering, as-if-serial semantics, sequential consistency, visibility, atomicity, happens-before, mutual exclusion, safe publication?

Java.Multithreading.What is “cooperative multitasking”? What type of multitasking does Java use? What is the reason for this choice?

🧠 What is Cooperative Multitasking?

🧩 Cooperative multitasking means:
Tasks (threads/processes) voluntarily yield control of the CPU so others can run.

🔧 How It Works:

  • The OS does not forcibly interrupt a running task.
  • It relies on each task to be nice and call yield() or similar.
  • If a task never yields, it can monopolize the CPU.

🛠 Used in:

  • Very early OSes (Windows 3.1, early MacOS)
  • Some embedded systems
  • Coroutines / fibers / green threads (e.g., Kotlin coroutines)
Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.What is “cooperative multitasking”? What type of multitasking does Java use? What is the reason for this choice?

Java.Multithreading.Can i adjust ThreadPoolExeccutor to work on different cores ?

❓ Can you adjust ThreadPoolExecutor to run on different cores?

⚠️ Short Answer: Not directly in Java — you cannot manually bind threads to specific cores using ThreadPoolExecutor or Java alone.

But…

✅ Long Answer: You can influence it indirectly and even hack it a bit if you’re determined 😎

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Can i adjust ThreadPoolExeccutor to work on different cores ?

Java.Multithreading.Is ForkJoin more effective than threadExecutor ?

✅ Yes, ForkJoinPool is more effective than ThreadPoolExecutor in certain scenarios — but not always.

Let’s dive into when and why ForkJoinPool outperforms ThreadPoolExecutor, and when it might not.

Continue reading →
Posted in Без рубрики | Comments Off on Java.Multithreading.Is ForkJoin more effective than threadExecutor ?