Posts

Showing posts from November 2, 2010

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 – Through the Runnable Interface

public class Test implements Runnable{
    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 Thread(new Test("firs…

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.

Recent Post

Recent Posts Widget