Thoughts on java in the thread

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

分类:Java 时间:2010-05-04 人气:277
分享到:
blog comments powered by Disqus

相关文章

  • java.lang.reflect dynamic proxy 2010-04-25

    public interface IReport { String published(); } public class Report implements IReport{ public String published() { System.out.println("hello~~~"); return "return Hello"; } } import java.lang.reflect.InvocationHandler; import java.lan

  • Java.lang.reflect.Proxy achieved by agents 2010-11-30

    With the article (to achieve a custom injection annotation function), which simulates a blocker. Found in the use of the time needed to call SimilarToTheInterceptor.scanAllFieldsAndMethods method If there is a need to inject more action, it is obviou

  • Xml parsing with dom4j reported java.lang.NoClassDefFoundError: org / jaxen / JaxenException 2011-01-06

    import java.io.File; import java.util.List; import org.dom4j.Document; import org.dom4j.io.SAXReader; public class Test { public static void main(String args[]) { try { File f = new File("D:/data.xml"); if (f.exists()) { SAXReader reader = new S

  • Java.lang.reflect简单讲解 2014-06-18

    在Java运行时刻,能否知道一个类的属性方法并调用改动之?对于任意一个对象,能否知道他的所属类,并调用他的方法?答案是肯定的.这种动态的获取信息及动态调用方法的机制在Java中称为"反射"(reflection). Java反射机制主要提供以下功能: 在运行时判断任意一个对象所属的类: 在运行时构造任意一个类的对象: 在运行时判断任意一个类所具有的成员变量和方法: 在运行时调用任意一个对象的方法. Reflection 是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程

  • Ticketing system, workflow 2010-07-02

    Ticketing system, the workflow description From 2009 to the end of June 2010, has been doing busy Shanghai-Nanjing high-speed rail project, and finally wait for the Shanghai-Nanjing high-speed rail on the line. Begin implementation of the project act

  • java.lang.Instrument 代理Agent使用 2014-12-30

    java.lang.Instrument包是在JDK5引入的,程序员通过修改方法的字节码实现动态修改类代码.这通常是在类的main方法调用之前进行预处理的操作,通过java指定该类的代理类来实现.在类的字节码载入JVM前会调用ClassFileTransformer的transform方法,从而实现修改原类方法的功能,实现AOP,这个的好处是不会像动态代理或者CGLIB技术实现AOP那样会产生一个新类,也不需要原类要有接口. (1) 代理 (agent) 是在你的main方法前的一个拦截器 (i

  • java.lang基础类库 2015-03-24

    1.常用的基础jar包 作为初学者,我觉得熟练掌握和应用java基础类库是很有必要的.因此,总结了一些自己在初学java时的一些基础类知识点.Java 2标准所提供的主要API如下表所示. 包名 内容概述 Java.applet 提供创建applet小程序所需要的类 Java.awt 包含用于创建用户界面和绘制图形图像的所有类 Java.io 提供与输入输出相关的类 Java.beans 包含与开发javaBeans相关的类 Java.lang 提供java语言程序设计的基础类 Java.net

  • About j2me java.lang.ClassNotFoundException: solution 2010-09-10

    Recent study JAVA games and application development, import always works when the sample reported java.lang.ClassNotFoundException:, try a lot of ways, and finally resolved, I am using pulsar eclipse environment First with the project --- clean No lo

  • java.lang包概述(1) 2012-03-09

    作者: 出处:Java研究组织 一.接口java.lang.Appendable 可追加(append)接口.实现了该接口的类的对象实例具有可向其追加字符或字符序列的能力.希望能够接收Formatter输出的类必须实现该接口. java.lang.CharSequence CharSequence是指可阅读的字符序列.该接口为只读访问各种类型的字符序列提供了统一的方法. java.lang.Cloneable 可 克隆接口.实现了该接口的类具有克隆的能力.可以通过Object.clone()方法

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.863 (s). 12 q(s)