Java Topics
HubJava Collections FrameworkCollections Practice ProgramsComparable vs ComparatorGenericsIteratorArrayListLinkedListStackVectorHashMapHashtableLinkedHashMapTreeMapArrayDequeDeque Double-Ended QueuePriorityQueueHashSetLinkedHashSetTreeSetAdapter PatternBuilder PatternFactory PatternMVC PatternObserver PatternSingleton PatternDynamic ProgrammingGraphHashingHeapLinked List DSAQueue DSARecursionSearching AlgorithmsSorting AlgorithmsStack DSATime ComplexityTreesException Handling Best PracticesCustom ExceptionException Hierarchyfinally BlockException Handlingthrow aur throwstry-catchBuffered StreamsByte StreamsCharacter StreamsDeserializationFile ClassJava NIO New I/OSerializationArrays in JavaArray OperationsArray Practice ProgramsArray Interview QuestionsJagged ArrayMulti-Dimensional ArraysOne Dimensional Arraybreak aur continuedo-while Loopfor Loopif-else StatementsNested Loopsswitch-case Statementwhile LoopJava Compilation ProcessJava ki FeaturesPehla Java ProgramHistory of JavaJava EditionsJava Program StructureJDK, JRE aur JVMJava Kya Hai?Immutable StringsString Class in JavaString Interview QuestionsString MethodsString Practice ProgramsStringBufferStringBuilderComments in JavaData Types in JavaIdentifiers in JavaInput & Output in JavaJava KeywordsOperators in JavaType Casting in JavaVariables in JavaJava Coding Interview QuestionsCollections Interview QuestionsCore Java Interview QuestionsJDBC Interview QuestionsMultithreading Interview QuestionsOOPs Interview QuestionsSpring Framework Interview QuestionsJava 8 Date/Time APIDefault aur Static Methods in InterfaceFunctional InterfaceLambda ExpressionMethod ReferenceOptional ClassStream APIBatch ProcessingCallableStatementJDBC ArchitectureJDBC — Java Database ConnectivityJDBC Practice ProjectsMySQL ConnectionPreparedStatementResultSetStatement InterfaceTransaction ManagementCreating ThreadsDaemon ThreadExecutor FrameworkInter-Thread CommunicationMultithreading Practice ProgramsRunnable InterfaceSynchronizationThread ClassMultithreading IntroductionThread Life CycleThread PriorityJava Cheat SheetImportant Formulas & Key ConceptsImportant Java ProgramsJava Quick RevisionAbstract ClassAbstractionAnonymous ClassClass and ObjectConstructorEncapsulationInheritanceInner ClassInterfaceMethod OverloadingMethod OverridingObject ClassObject CloningObject-Oriented Programming OOPPolymorphismstatic Keywordsuper Keywordthis KeywordWrapper ClassesArray Practice ProgramsBasic Java Practice ProgramsCollection Practice ProgramsJDBC Practice ProgramsMultithreading Practice ProgramsOOPs Practice ProgramsPlacement Coding QuestionsString Practice ProgramsBanking SystemChat ApplicationE-Commerce Backend — Spring Boot REST APIEmployee Management SystemLibrary Management SystemSpring Boot Fullstack ProjectStudent Management SystemCookies in ServletExpression Language ELGenericServletHttpServletJavaServer Pages JSPJSP TagsMVC ArchitectureHttpServletRequest & HttpServletResponseServlet IntroductionServlet Life CycleSession TrackingDependency InjectionSpring BeansSpring CoreSpring FrameworkAPI GatewaySpring Cloud Config ServerDocker DeploymentEureka Server — Service DiscoverySpring Boot CRUD ApplicationException Handling in Spring BootJWT AuthenticationREST API in Spring BootSpring SecuritySpring BootSpring Data JPAValidation in Spring Boot
Creating Threads
Last Updated : 26 May, 2026
title: Creating Threads in Java
Multithreading59 words6 headingsTables includedExamples included
title: Creating Threads in Java description: Java mein threads banane ke sabhi tarike
Method 1: Thread Class Extend
class PrintNumbers extends Thread {
private int start, end;
PrintNumbers(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public void run() {
for (int i = start; i <= end; i++) {
System.out.println(getName() + ": " + i);
}
}
}
PrintNumbers t1 = new PrintNumbers(1, 5);
PrintNumbers t2 = new PrintNumbers(6, 10);
t1.setName("Thread-1");
t2.setName("Thread-2");
t1.start();
t2.start();
Method 2: Runnable Interface
java exampleWoHoTech
class PrintEven implements Runnable {
@Override
public void run() {
for (int i = 2; i <= 20; i += 2)
System.out.println("Even: " + i);
}
}
Thread t = new Thread(new PrintEven(), "EvenThread");
t.start();Method 3: Lambda (Java 8+)
Thread t = new Thread(() -> {
for (int i = 1; i <= 5; i++)
System.out.println("Lambda thread: " + i);
}, "LambdaThread");
t.start();
Method 4: Callable + FutureTask
Return value chahiye ho.
import java.util.concurrent.*;
Callable<Integer> task = () -> {
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;
return sum;
};
FutureTask<Integer> future = new FutureTask<>(task);
Thread t = new Thread(future);
t.start();
int result = future.get(); // Blocking, wait for result
System.out.println("Sum: " + result); // 5050
Method 5: ExecutorService (Recommended)
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
final int taskId = i;
executor.submit(() -> System.out.println("Task " + taskId + " by " + Thread.currentThread().getName()));
}
executor.shutdown();
Runnable vs Callable
| Runnable | Callable | |
|---|---|---|
| -- | -- | -- |
| Return | void | V (generic) |
| Exception | Checked nahi | Checked allowed |
| Method | run() | call() |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Creating Threads.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, multithreading
Related Java Topics
Continue learning this concept
MultithreadingDaemon Threadtitle: Daemon Thread in JavaMultithreadingInter-Thread Communicationtitle: Inter-Thread CommunicationMultithreadingThread Classtitle: Thread Class in JavaMultithreadingMultithreading Introductiontitle: Thread IntroductionMultithreadingThread Life Cycletitle: Thread Life CycleMultithreadingThread Prioritytitle: Thread Priority in Java