Posts

daemon thread

What is a daemon thread            Daemon threads are sometimes called "service" threads. These are threads that normally run at a low priority and provide a basic service to a program or programs when activity on a machine is reduced.           An example of a daemon thread that is continuously running is the garbage collector thread. This thread is provided by the JVM.

difference between yield and sleeping

What is the difference between yield and sleeping When a task invokes yield(), it changes from running state to runnable state. When a task invokes sleep(), it changes from running state to waiting/sleeping state.

Thread Life Cycle in java

Image
Thread Life Cycle -      In the following figure, we are describing about Thread Life Cycle. 1) Create Thread – Thread can be create through Thread Class or Runnable interface. 2) New State – when any object of thread is being made by new Thread() 3) Runnable – When start method of thread is called means thread is now in runnable state. 4) Running – After Runnable state, based on the priorities of thread, is being to running state. 5) Dead – After running state, thread will go to Dead. Or it can be block states. 6) Block States – Thread can be in block state by calling wait(), sleep(), join() methods. 7) Sleep State – when sleep() method is called by thread, thread will go to the sleep state. Sleep State means, This thread is still alive and it can be go on runnable state. Thread has sleep method for passing the time in millisecond. And throw InterruptedException.     static void sleep(long millisecond) throws Int...

Creating a Java Thread example

Creating A Thread - There are two ways to create threads. 1)extends Thread Class 2)implements Runnable Interface. Example – Through the Thread Class public class Test extends Thread{     private String msg;         Test(String msg){         this.msg=msg;     }     public void run() {         for(int x=0; x<5; x++){             System.out.println("-----"+msg);         }     }     public static void main(String[] args){         Thread thread1 = new Test("first Thread");         Thread thread2 = new Test("Second Thread");         thread1.start();         thread2.start();     } }  Example...

about Process And Thread java

Process And Thread When program runs under the concurrency environment, Then Thread will be in lead roles. Environment which manages some threads called process. Threads – Thread are light weight piece of code which runs under the concurrent environment. Many threads can run concurrently and share the same heap memory but every thread has its own stack allocation area. Threads can run under the process. Process – Process is an execution of program and has its own memory allocation areas. A JVM runs in a single process and threads runs under the JVM and share a single heap memory allocation area. Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication.

about user defined exception in Java

User defined exceptions may be implemented by defining a new exception class by extending the Exception class. public class MyException extends Exception { /* class definition of constructors goes here */ public MyException() { super(); } public MyException (String errorMessage) { super (errorMessage); } } Throw and/or throws statement is used to signal the occurrence of an exception. Throw an exception: throw new MyException(“I threw my own exception.”) To declare an exception: public myMethod() throws MyException {…}

about Exceptions in Java

Exceptions in Java -     An Exception is phenomenal condition, which can comes your program when program is executed. And this condition interrupt with program's normal flow.     When any program face the exceptional condition, an exception object is made by JVM and program will be terminate.             For prevent the termination of program, There is need to handle this situation.     There are three type of exceptional event occurs.     1)Error 2)Checked Exceptions 3)Unchecked Exceptions Errors - Error can be occurs for network failure, Hardware failure or any other reason besides your programs. So you can not handle this through program code. Checked Exceptions -     Java compiler checks your code at compile time. If any exception occurs in this time error handler read this at compile time. This is called checked exceptions.   ...

Recent Post

Recent Posts Widget