Thoughts on java in the thread

sponsored links
Thoughts on java in the thread

1. The difference between processes and threads

Simple language, it said that the implementation process is a process, and thread can be understood as the process of implementation of a program fragment.

Man with a little word that is, each process has its own code and data space (process context); the thread can be seen as lightweight process. In general, (without using special techniques), the thread generated by the same process share the same piece of memory space.

The same process two pieces of code can not run concurrently, unless the introduction of the thread.

Threads are processes that when the process exit produced by the process thread will be forced to exit and clear.
Thread take up less resources to the process of occupied resources.
Processes and threads can have priority.
In the process the thread is a thread system. The process can be understood as a program the first thread.

Multi-process ----- in the operating system can run multiple tasks (programs).

Multi-thread ----- in the same application, there are multiple sequential streams simultaneously.

2. By rail ticketing program to understand multi-threaded two ways: through the java.lang.Thread class and the Runnable interface, through

There are two java multi threaded way. One is the direct successor to Thread class, Runnable interface, the second is to achieve. Then the two way multi-threaded application on any difference?

To answer this question, we can write some code for analysis. We use the code to simulate the railway ticketing system, to achieve through a four day ticket outlets 100 in a second train ticket, a ticket with a thread, said.

We first write the program that:

public class ThreadDome1 (
public static void main (String [] args) (
ThreadTest t = new ThreadTest ();
t.start ();
t.start ();
t.start ();
t.start ();
)
)

class ThreadTest extends Thread (
private int ticket = 100;
public void run () (
while (true) (
if (ticket> 0) (
System.out.println (Thread.currentThread (). GetName () +
"Is saling ticket" + ticket -);
) Else (
break;
)
)
)
)

The above code, we ThreadTest Class Analogue ticket ticketing process, run method of each cycle will be the total number of votes by 1, analog to sell a ticket, also print out the ticket number, direct the remaining votes to zero so far. In ThreadDemo1 class main method, we create a thread object, and repeat the start four times, hoping in this way to produce four threads. The results from the operation perspective we find that there is only one thread is running, this result tells us: a thread can start a thread object, no matter how many times you call start () method, found that only one thread.

We then modify the ThreadDemo1, in four main ways to create a Thread object:

public class ThreadDemo1 (
public static void main (String [] args) (
new ThreadTest (). start ();
new ThreadTest (). start ();
new ThreadTest (). start ();
new ThreadTest (). start ();
)
)

class ThreadTest extends Thread (
private int ticket = 100;
public void run () (
while (true) (
if (ticket> 0) (
System.out.println (Thread.currentThread (). GetName () +
"Is saling ticket" + ticket -);
) Else (
break;
)
)
)
)

This time an end yet?

Judging from the results of each polling numbers are printed four times that of four threads each sell 100 tickets each, not to sell 100 tickets together. This is how it caused? What we need is to deal with multiple threads of a resource, a resource can only correspond to an object, in the above program, we created four ThreadTest object, it means creating the four resources, each resource has a 100 tickets, each thread in its own handling of their resources.

Through these experiments and analysis, can be summed up in order to achieve this railway ticketing process, we can only create a resource object, but to create multiple threads to deal with a resource object, and each thread is running the same program code. In the review prepared to use interface, multi-threaded process.

public class ThreadDemo1 (
public static void main (String [] args) (
ThreadTest t = new ThreadTest ();
new Thread (t). start ();
new Thread (t). start ();
new Thread (t). start ();
new Thread (t). start ();
)
)

class ThreadTest implements Runnable (
private int tickets = 100;
public void run () (
while (true) (
if (tickets> 0) (
System.out.println (Thread.currentThread (). GetName () +
"Is saling ticket" + tickets -);
)
)
)
)

The above program, create four threads, each thread calls the object in the same ThreadTest run () method to access the same object in the variable (tickets) for instance, the program meets the needs of our . Can start the Windows Notepad program, like many, that is, multiple processes use the same program code in Notepad.

Shows that to achieve Runnable interface, relative to the Thread class inheritance, there are the following significant benefits:

(1) the same code for multiple threads to deal with the same resources, the virtual CPU (thread) with the program code, data, and effective separation, better reflects the object-oriented design.

(2) can eliminate the characteristics of Java's single inheritance brings limitations. We often come across such a situation in which we have inherited to a certain subclass into a multi-threaded, because a class can not simultaneously have two parent, it is not the way with a succession of Thread class, then This class can only achieve Runnable interface by way of the.

(3) favor the robust procedure, the code can be shared by multiple threads, code and data is independent. When multiple threads execute the code from the same instance of the class, that is that they share the same code. Operating the same data multiple threads, the code has nothing to do with them. When sharing access to the same object, that is, they share the same data. The time when the thread is constructed, it needs the code and data through an object as a constructor function Real arguments to go, this is a realization that object Runnable interface, the class Instance .

Article reproduced from: http://news.newhua.com/news1/program_net/2009/29/09291444214H0095KI32IIB72BEA9CDG7GFH95729HI19CJ5KCKI133.html
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Thoughts on java in the thread

  • NoClassDefFoundError: javax / servlet / Servlet

    In the project in order to achieve a listener in web.xml set up a listener, did start in Tomcat actually occurred after java.lang.NoClassDefFoundError: javax / servlet / ServletContextListener this anomaly google and found the reasons for the lack of serv

  • How the primary key agent-hibernate

    Existing Table A, B Statement: A, B field id-based keys. A: [id, name] B: [id, Aid, title] B and A's set up a many-to-one relationship because of A, B two tables are the primary key id field so add B when Hibernate will automatically retrieve id ...

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Message Dri

  • java read file

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa

  • Hibernate primary key strategy-sequence

    Today, the use of hibernate in the company encountered a troublesome problem, the use of hibernate when the primary key generation strategy set sequence, but always reported in the implementation could not get next sequence value of the error, then o ...

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Great collection of java interview topics

    1, object-oriented features of what has 1. Abstract: Abstract is that it has overlooked a subject has nothing to do with the current goal of those aspects in order to more fully with the current objectives of the attention-related aspects. Abstract does n

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries