You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
Java is the only high-level language that provides API and has support for Multithreading.
isAlive function returns true or false (bool) for active/dead states in threads.
Multiprogramming
Context switching is costly in terms of computing and memory requirements.
Multitasking
Interleaved execution of more than 1 task.
It is heavy-weight software.
Context switching is necessary for multi-tasking.
Multithreading
Light Weight, Thread based overlapped execution.
No need for Context switching.
Thread means no execution point.
Whenever we use Multitasking,Multiprogramming and Multithreading is used there is a PROBLEM Dependency between statement. The SOLUTION is to execute it in a synchronized manner or Synchronized thread.
Interthread communication and synchronized communication.
Thread States
|New|->Start()->|(Runnable[Ready to CPU])(Running[Assigned to CPU])|->Dead State.
Running is currently in excecution whereas runable isn't.
All states are represented as final static constants.
|New|->Start()->|(Runnable[Ready to CPU])(Running[Assigned to CPU])|-stop-> Terminated State.
Suspend function temporarily blocks a thead.
Program 1a: Simple Threading Program
/* Simple Program using Threading */classMainThread{
publicstaticvoidmain(String[] args) {
Threadt=Thread.currentThread();
System.out.println(t.getName()+"..."+t.getState()+"..."+t.getPriority()+"..."+t.isAlive());
}
}
Output:-
main...RUNNABLE...5...true
Program 1b:-
/* Simple Program using Threading */classMainThread{
publicstaticvoidmain(String[] args) {
Threadt=Thread.currentThread();
System.out.println(t.getName()+"..."+t.getState()+"..."+t.getPriority()+"..."+t.isAlive());
try{
for (inti=0;i<10;i++){
System.out.println(t.getName()+"...i..."+i);
Thread.sleep(1000);
}
}catch(InterruptedExceptione){
System.out.println("..."+e);
}
}
}
[user]$ javap java.lang.Thread
Compiled from "Thread.java"
public class java.lang.Thread implements java.lang.Runnable {
java.lang.ThreadLocal$ThreadLocalMap threadLocals;
java.lang.ThreadLocal$ThreadLocalMap inheritableThreadLocals;
volatile java.lang.Object parkBlocker;
public static final int MIN_PRIORITY;
public static final int NORM_PRIORITY;
public static final int MAX_PRIORITY;
long threadLocalRandomSeed;
int threadLocalRandomProbe;
int threadLocalRandomSecondarySeed;
static void blockedOn(sun.nio.ch.Interruptible);
public static native java.lang.Thread currentThread();
public static native void yield();
public static native void sleep(long) throws java.lang.InterruptedException;
public static void sleep(long, int) throws java.lang.InterruptedException;
public static void onSpinWait();
protected java.lang.Object clone() throws java.lang.CloneNotSupportedException;
public java.lang.Thread();
public java.lang.Thread(java.lang.Runnable);
java.lang.Thread(java.lang.Runnable, java.security.AccessControlContext);
public java.lang.Thread(java.lang.ThreadGroup, java.lang.Runnable);
public java.lang.Thread(java.lang.String);
public java.lang.Thread(java.lang.ThreadGroup, java.lang.String);
public java.lang.Thread(java.lang.Runnable, java.lang.String);
public java.lang.Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String);
public java.lang.Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String, long);
public java.lang.Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String, long, boolean);
public synchronized void start();
public void run();
public final void stop();
public void interrupt();
public static boolean interrupted();
public boolean isInterrupted();
public final native boolean isAlive();
public final void suspend();
public final void resume();
public final void setPriority(int);
public final int getPriority();
public final synchronized void setName(java.lang.String);
public final java.lang.String getName();
public final java.lang.ThreadGroup getThreadGroup();
public static int activeCount();
public static int enumerate(java.lang.Thread[]);
public int countStackFrames();
public final synchronized void join(long) throws java.lang.InterruptedException;
public final synchronized void join(long, int) throws java.lang.InterruptedException;
public final void join() throws java.lang.InterruptedException;
public static void dumpStack();
public final void setDaemon(boolean);
public final boolean isDaemon();
public final void checkAccess();
public java.lang.String toString();
public java.lang.ClassLoader getContextClassLoader();
public void setContextClassLoader(java.lang.ClassLoader);
public static native boolean holdsLock(java.lang.Object);
public java.lang.StackTraceElement[] getStackTrace();
public static java.util.Map<java.lang.Thread, java.lang.StackTraceElement[]>getAllStackTraces();
public long getId();
public java.lang.Thread$StategetState();
public static void setDefaultUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler);
public static java.lang.Thread$UncaughtExceptionHandlergetDefaultUncaughtExceptionHandler();
public java.lang.Thread$UncaughtExceptionHandlergetUncaughtExceptionHandler();
public void setUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler);
static void processQueue(java.lang.ref.ReferenceQueue<java.lang.Class<?>>, java.util.concurrent.ConcurrentMap<? extends java.lang.ref.WeakReference<java.lang.Class<?>>, ?>);
static {};
}