|  Home   |  Contact Us   |  Online Exam   |  Tech World   |  Jobs    |  Member Login
   java lang basics
   String

  String Length
  String Concatenation
  charAt()
  getChars()
  getBytes()
  toCharArray()
  equals()
  equalsIgnoreCase()
  startsWith()
  endsWith()
  compareTo()
  substring()
  concat()
  replace()
  trim()

   Stringbuffer

  setLength()
  setChars()
  charAt()
  getChars()
  append()
  insert()
  reverse()
  delete()
  deleteCharAt()
  compareTo()
  substring()
  replace()

   Thread

  start()
  suspend()
  resume()
  wait()
  sleep()
  join()
  alive()
  isalive()
  yield()
  setPriority()
  getPriority()
  synchronization()

   Wrapper Class
   String Tokenizer
Lang Package - Thread
Home  |  Lang Basics  |   String  |  StringBuffer  |   Thread  |   Wrapper Class  |  String Tokenizer

Thread:
A thread is a one unit of execution in a program
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

  »  The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.

  »  All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows:
Example:
public class Threads{
public static void main(String[] args){
Thread th = new Thread();
System.out.println("Numbers are printing line by line after 5 seconds : ");
try{
for(int i = 1;i <= 10;i++)
{
System.out.println(i);
th.sleep(5000);
}
}
catch(InterruptedException e){
System.out.println("Thread interrupted!");
e.printStackTrace();
}
}
}
Output:
Numbers are printing line by line after 5 seconds :
1
2
3
4
5
6
7
8
9
10
Start():
public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Block until the other threads finish.
thr2.join();
thr3.join();
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}
Output:

Starting Thread-0...
Starting Thread-1...
Active threads = 3
Thread-0 has finished executing.
Thread-1 has finished executing.
Suspend():
public final void suspend()
Suspends this thread.
First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException (in the current thread).
If the thread is alive, it is suspended and makes no further progress unless and until it is resumed.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Suspend the secondary thread.
System.out.println("Suspending " + thr2.getName() + "...");
thr2.suspend();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Block until the other threads finish or until
// the secondary thread executes for 5000 ms.
thr2.join(5000);
thr3.join();
// Resume the secondary thread.
System.out.println("Resuming " + thr2.getName());
thr2.resume();
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}
Output:
Starting Thread-0...
Suspending Thread-0...
Starting Thread-1...
Active threads = 3
Thread-1 has finished executing.
Resuming Thread-0
Sleep():
public static void sleep(long millis) throws InterruptedException

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Sleep for 5 seconds (5000 milliseconds // and 0 nanosecondss).
Thread.sleep(5000, 0);
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Block until the other threads finish.
thr2.join();
thr3.join();
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}
Output:
Starting Thread-0...
Thread-0 has finished executing.
Starting Thread-1...
Active threads = 2
Thread-1 has finished executing.
Resume():
public final void resume()

Resumes a suspended thread.
First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException (in the current thread).

If the thread is alive but suspended, it is resumed and is permitted to make progress in its execution.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Suspend the secondary thread.
System.out.println("Suspending " + thr2.getName() + "...");
thr2.suspend();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Block until the other threads finish or until
// the secondary thread executes for 1000 ms.
thr2.join(1000);
thr3.join();
// Resume the secondary thread.
System.out.println("Resuming " + thr2.getName());
thr2.resume();
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}
Output:

Starting Thread-0...
Suspending Thread-0...
Starting Thread-1...
Active threads = 3
Thread-1 has finished executing.
Resuming Thread-0
Join():
public final void join(long millis, int nanos) throws InterruptedException

Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Block until the other threads finish.
thr2.join();
System.out.println("No longer blocking on thread " + thr2.getName());
thr3.join();
System.out.println("No longer blocking on thread " + thr3.getName());
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
Thread currThread = Thread.currentThread();
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(currThread.getName() + " has finished executing.");
}
private int counter = 0;
}
Output:
Starting Thread-0...
Starting Thread-1...
Active threads = 3
Thread-0 has finished executing.
Thread-1 has finished executing.
No longer blocking on thread Thread-0
No longer blocking on thread Thread-1
isAlive():
public final boolean isAlive()

Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Determine which threads are still alive.
if (thr2.isAlive())
{
System.out.println(thr2.getName() + " is alive.");
}
else
{
System.out.println(thr2.getName() + " is NOT alive.");
}
if (thr3.isAlive())
{
System.out.println(thr3.getName() + " is alive.");
}
else
{
System.out.println(thr3.getName() + " is NOT alive.");
}
// Block until the other threads finish.
thr2.join();
thr3.join();
// Determine again which threads are still alive.
if (thr2.isAlive())
{
System.out.println(thr2.getName() + " is alive.");
}
else
{
System.out.println(thr2.getName() + " is NOT alive.");
}
if (thr3.isAlive())
{
System.out.println(thr3.getName() + " is alive.");
}
else
{
System.out.println(thr3.getName() + " is NOT alive.");
}
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}
Output:
Starting Thread-0...
Starting Thread-1...
Active threads = 3
Thread-0 is alive.
Thread-1 is alive.
Thread-0 has finished executing.
Thread-1 has finished executing.
Thread-0 is NOT alive.
Thread-1 is NOT alive.
yield():
public static void yield()

Causes the currently executing thread object to temporarily pause and allow other threads to execute.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Block until the other threads finish.
thr2.join();
thr3.join();
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 10000000; i++)
{
// Yield control to another thread every
// 1000000 iterations.
if ((i % 1000000) == 0)
{
System.out.println(Thread.currentThread().getName() + " is yielding control...");
Thread.currentThread().yield();
}
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}
Output:
Starting Thread-0...
Starting Thread-1...
Thread-0 is yielding control...
Active threads = 3
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 is yielding control...
Thread-0 is yielding control...
Thread-1 has finished executing.
Thread-0 has finished executing.
Run():
public void run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

Subclasses of Thread should override this method.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
// Create a second thread and run it.
Thread thr2 = new Thread(this);
System.out.println("Running " + thr2.getName() + "...");
thr2.run();
// Create a third thread and run it.
Thread thr3 = new Thread(this);
System.out.println("Running " + thr3.getName() + "...");
thr3.run();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads after run = " + Thread.activeCount());
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}

Output:
Running Thread-0...
main has finished executing.
Running Thread-1...
main has finished executing.
Active threads after run = 1
Stop():
public final void stop()

Forces the thread to stop executing.

If there is a security manager installed, its checkAccess method is called with this as its argument. This may result in a SecurityException being raised (in the current thread).

If this thread is different from the current thread (that is, the current thread is trying to stop a thread other than itself), the security manager's checkPermission method (with a RuntimePermission("stopThread") argument) is called in addition. Again, this may result in throwing a SecurityException (in the current thread).
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// The threads are taking a long time.
thr2.stop();
System.out.println(thr2.getName() + " has been manually stopped.");
thr3.stop();
System.out.println(thr3.getName() + " has been manually stopped.");
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < Integer.MAX_VALUE; i++)
{
// Just increment a counter.
counter++;
}
System.out.println(Thread.currentThread().getName() + " has finished executing.");
}
private int counter = 0;
}

Output:
Starting Thread-0...
Starting Thread-1...
Active threads = 3
Thread-0 has been manually stopped.
Thread-1 has been manually stopped.
SetPriority():
public final void setPriority(int newPriority)

Changes the priority of this thread.

First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.

Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group.
Example:
public class Program implements Runnable
{
public static void main(String[] args)
{
Program p = new Program();
p.init();
}
public void init()
{
try
{
// Create a second thread and start it.
Thread thr2 = new Thread(this);
thr2.setPriority(Thread.MAX_PRIORITY);
System.out.println("Starting " + thr2.getName() + "...");
thr2.start();
// Create a third thread and start it.
Thread thr3 = new Thread(this);
thr3.setPriority(Thread.NORM_PRIORITY + 1);
System.out.println("Starting " + thr3.getName() + "...");
thr3.start();
// Display the number of active threads. This
// count includes the main thread.
System.out.println("Active threads = " + Thread.activeCount());
// Block until the other threads finish.
thr2.join();
thr3.join();
}
catch (InterruptedException ex)
{
System.out.println(ex.toString());
}
}
// Implements Runnable.run()
public void run()
{
for (int i = 0; i < 100000000; i++)
{
// Just increment a counter.
counter++;
}
// Display the name of the thread executing this code.
System.out.println(Thread.currentThread().getName() +
" [priority = " + Thread.currentThread().getPriority() + "] has finished executing.");
}
private int counter = 0;
}

Output:
tarting Thread-0...
Starting Thread-1...
Active threads = 3
Thread-0 [priority = 10] has finished executing.
Thread-1 [priority = 6] has finished executing.
Back to top
All Rights Reserved : skdotcom technologies