Home Answers Viewqa Java-Beginners Thread and Process

 
 


vijayababu
Thread and Process
1 Answer(s)      5 years and a month ago
Posted in : Java Beginners

View Answers

May 19, 2008 at 8:21 PM


Hi vijayaBabu


A process can contain multiple threads. In most multithreading operating systems, a process gets its own memory address space; a thread doesn't. Threads typically share the heap belonging to their parent process. For instance, a JVM runs in a single process in the host O/S. Threads in the JVM share the heap belonging to that process; that's why several threads may access the same object. Typically, even though they share a common heap, threads have their own stack space.


This is thread code,

public class ThreadExample extends Thread {
public ThreadExample(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch(InterruptedException e){
System.out.println(e);
}
}
System.out.println("Complete! " + getName());
}
public static void main (String[] args) {
new ThreadExample("Roseindia").start();

}
}

---------------------------------------

Use of process, you will see in this example.

import java.io.*;

public class ProcessExample {

public static void main(String args[]) {
String s = null;
try {
// run the Unix "ps -ef" command
Process process = Runtime.getRuntime().exec("ps -ef");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception error: ");
e.printStackTrace();
System.exit(-1);
}
}
}

-------------------------------------------

Read for more information.

http://www.roseindia.net/java/









Related Pages:
Thread
Thread  What is multi-threading? Explain different states of a thread... a program or a process to execute many tasks concurrently (at the same time and parallel). It allows a process to run its tasks in parallel mode on a single
thread runtime process
thread runtime process  Java thread runtime process
Thread and Process - Java Beginners
Thread and Process  Dear Deepak Sir, What is the diffrence between Thread and Process.Give an example with explanation. Thnaks & Regards, VijayaBabu.M  Hi vijayaBabu A process can contain multiple threads
Java Thread Synchronization - Development process
Java Thread Synchronization  Hi,Please help me with this coding. I... press any key on the keyboard,then one thread must stop.When I press the keys second time,then the second thread must stop. Please provide me with the code  
Java thread
Java thread  What is the difference between process and thread
Java Thread
Java Thread       A java thread is an execution context or a lightweight process. It is a single sequential flow of control within a program. Programmer may use java thread mechanism
Extending thread - Java Beginners
that has only one thread is referred to as a single-threaded process, while...Extending thread  what is a thread & give me the programm of exeucte the thread   Hi friend, Thread : A thread is a lightweight
Inter Thread Communication
Inter Thread Communication  what is inter thread communication?   hi friend, Inter thread communication is a process of communication between two threads. In this process, a thread outside the critical section is tried
Overview of Thread
with this program. Thread A thread is a lightweight process which exist within... thread is referred to as a single-threaded process, while a process... process.  Main Thread When any standalone application is running
Thread in java
A thread is a lightweight process which exist within a program and executed... with a single process. Thus a process that has only one thread is referred to as a single-threaded process, while a process with multiple threads is referred
thread
thread  can parent thread be dead if child thread is not dead
Thread
Thread  Thread Life Cycle
Thread
Thread  what is the use of thread
Green Thread - Java Beginners
of Green Thread in java. Thanks in advance...  Hi friend Green threads... on Linux at one point (since you don't have to spawn a process for each native thread), but VM technology has advanced significantly since version 1.1 and any
Thread priority in java
Thread priority in java A thread is a part or entity of a  process... language which means JVM allow an application to have multiple thread running concurrently. In java each and every thread has priority , priority means which
Thread
Thread  Explain two ways of creating thread in java. Explain at three methods of thread class.   Java Create Thread There are two main ways of creating a thread. The first is to extend the Thread class and the second
Java Thread destroy
Java Thread destroy In this tutorial, we are using Thread.destroy() method to destroy the thread. Thread destroy() : Thread class provides destroy method to destroy the thread. In general, Thread.destroy() is dangerous
Thread
to the thread constructor eventhough we had created only one thread and if you say we have added to point to the current thread then why we have not added this in the following line "s=s1" Pls reply...... class MyThread extends Thread { Thread
Thread
Thread  what happen when we call the Wait(),Notify() and NotifyAll() methods in the Thread
Thread
Thread  class Extender extends Thread { Extender(Runnable run...(); } public void run(){ System.out.println("Extender Thread is Started :"); //new Thread(new Implementer()).start(); } } class Implementer
Thread
Thread  will this code work..? class A extends Thread { public...=" + i); } public static void main(string args[]) { A a = new A(); Thread t = new thread(a); t.start(); } } Is it possible to run above program with out
Java Thread Interrupted
Java Thread Interrupted In this tutorial, you will learn how to interrupt a thread with example in Java. Thread Interrupt : Java thread facilitate you to interrupt any thread. Interrupting a thread means to stop the running thread
Thread
Thread  Write a Java program to create three theads. Each thread should produce the sum of 1 to 10, 11 to 20 and 21to 30 respectively. Main thread....   Java Thread Example class ThreadExample{ static int
Thread
Thread   there are two threads running at a time.. when am updating a values in database. both thread halt and stop for moment till it get updated into database... so i dnt want thread to get halts for tht moment of period. whats
Java :Thread Join
Java :Thread Join In this tutorial you will see how to use join method in java thread. join() method - join method waits until the thread die. If we are using multiple threads and want to continue to next process only after
J2ME Thread Processing Example
:- Finished executing code. In the output First of all process() thread... J2ME Thread Processing Example       In the given example, you will learn about the thread and how
java code - Development process
java code  hi, this is sampath, how to acchive the batch processing in java or batch process base java thread. can u send the code and brief explanation. thanks and regards sampath reddy
Daemon process in j2me
Daemon process in j2me  i want to create daemon thread in j2me, can u help me in creating the midlet for that?   i mean, i want to create daemon process, which keeps on running and check if any sms has come
Java Thread not geting Connection pool - JSP-Servlet
. There is on background process which will get execute when user click button from browser. To execute that process I have used Thread. But there is an problem...Java Thread not geting Connection pool  Hi All, Please help me
Confuse about Quartz or Thread. - JSP-Servlet
executing certain system process on given schedules. Thread : A thread.... Thus a process that has only one thread is referred to as a single-threaded... process. In Java Programming language, thread is a sequential path of code
Synchronization in cluster - Development process
.  Hi, Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can..., it is possible for one thread to modify a shared object while another thread
regarding socket - Development process
regarding socket  sir I have a project of socket. which takes input from user(client) and checks in database(server) and sends the responses to client but I am facing problem this connection maintains for only single thread
java multithread - Development process
Thread { Socket m_clientSocket; int m_clientID = -1...("Stopping client thread for client :" + m_clientID
Thread in java
Thread in java  which method will defined in thread class
T - Java Terms
; Java Thread A java thread is an execution context or a lightweight process. It is a single sequential flow of control within a program. Programmer may use java thread mechanism to execute multiple tasks at the same time
Thread restart
Thread restart  hello,, can dead thread restart?   hii, A dead Thread can't restart not even by calling start() again on that object
Daemon thread
Daemon thread  what is need of daemon thread
java Thread
java Thread  what is purpose of Thread
Daemon thread
Daemon thread  what is need of daemon thread
Java thread
Java thread  How can a dead thread be restarted?  A dead thread cannot be restarted
Java thread
Java thread  What's the difference between a thread's start() and run() methods
Thread method
Thread method  What is the purpose of the wait(), notify(), and notifyAll() methods
Java thread
Java thread  What invokes a thread's run() method
Java thread
Java thread  What are the ways in which you can instantiate a thread
Java thread
Java thread  What are the high-level thread states
Demon thread
Demon thread  What is demon thread? why we need Demon thread?  ... there are daemon thread by killing them abruptly.Any thread can be a daemon thread.../thread/daemon-threads.shtml