Threads and processes

Source: Internet
Author: User
Tags thread class ticket

String: Represents an immutable class, which is very common: judging the creation of several string objects: string s = new String ("Ooxx");        1 or two: if "Ooxx" is already present in the constant pool, create a string object directly if there is no "Ooxx" in the constant pool, create a "Ooxx" in the constant pool and create a string object in the heap;  String s = "A" + "B" + "C";    When compiling, turn "a" + "B" + "C" into "ABC"; String s2 = s + "AA", the common method of creating "ABCAA" during run time see API;

Stringbuffer/stringbuilder: Represents a variable string;  Stringbuffer/stringbuilder difference: StringBuffer: Thread-safe, suitable for multi-threading, but performance is a bit low; StringBuilder (JAVA5): thread insecure, suitable for single-threaded, high performance a bit;    The official recommended append (Object O): Indicates that a new content is appended to the contents of its object, applied to the spelling string, and its performance is much higher than string; Common methods see API;

Math: Encapsulates a lot of math formulas, algorithms that are;  Constant: Double e = MATH.E;    Double pi = Math.PI;    Pseudo-Random number: Math.random ();//Generate [0.0,1.0] a random number between static imports: Imports are static members of a class: import static Java.lang.Math.PI; Import static java.lang.math.*;

Random:new random (). nextxxx (int max),//Represents a pseudo-random number uuid between [0,max]: A unique string that represents a 128-bit uuid uuid = Uuid.randomuuid ();    String uid = uuid.tostring (); Suitable for the primary key to make the database;

BigInteger: Large integer: Create object: New BigInteger (String val);    BigInteger Add (BigInteger otherval); BigInteger Subtract (BigInteger val) BigInteger multiply (BigInteger val) BigInteger divide (BigInteger val) Big      Decimal: Data that can represent arbitrary precision: New BigDecimal (double val);//still cannot accurately represent data new BigDecimal (String val);//can be precise, suitable for financial money, precision requirements of high data    BigDecimal Add (BigDecimal otherval);  BigDecimal Subtract (BigDecimal val) BigDecimal multiply (BigDecimal val) BigDecimal divide (BigDecimal val) { Private Bigdecaimal total;//Total amount

}

A process is an application that runs in memory, each of which has its own separate piece of memory space, and can have multiple threads in a process. For example, in a Windows system, a running Xx.exe is a process.
Java program has several threads in the process: main thread, garbage collection thread (background thread)
A thread is an execution task (a control unit) in a process in which multiple threads can be run and multiple threads can share data.
Multi-process: Multiple programs running concurrently in the operating system;
Multithreading: Multiple tasks that run concurrently in the same process;
A process has at least one thread, and in order to improve efficiency, multiple control units can be opened in one process.
Run concurrently. such as: multi-threaded download software.
It can be run at the same time, but the results of the program run show that, while running at the same time, each result is inconsistent.
Because multithreading has one feature: randomness.
Cause: The CPU is constantly switching to handle each thread in an instant.
Can be understood as multiple threads in the scramble for CPU resources.

Threads have the characteristics of many traditional processes and are also referred to as light processes (Light-weight process) or process elements;
The traditional process is called a heavy process (heavy-weight), which is equivalent to a task with only one thread. In the operating system in which threading is introduced, typically a process has several threads and at least one thread is required.

The difference between a process and a thread:
1. The process has a separate process space, and the data storage space (heap space and stack space) in the process is independent.
2. The thread's heap space is shared, the stack space is independent, and the thread consumes less resources than the process, which can affect each other.

Inherit the thread class
Subclasses overwrite the Run method in the parent class and store the code running by the thread in run.
The same thread that creates the subclass object is also created.

Package thread;

/** * Create a thread the second way: * * 1. Define a class Mythread implement the Runnable interface 2. Implement the Run Method 3. Thread object creation, new thread (Runnable object) * 4. The start method of the thread class is still called;    * */class MyThread2 implements Runnable {@Override public void run () {//thread body for (int i = 0; i <; i++) {    String name = Thread.CurrentThread (). GetName ();   System.out.println (name+ "--" + i); }  } }

public class ThreadDemo2 {public static void main (string[] args) {

for (int i = 0; i <; i++) {

System.out.println ("main-->" +i);    if (i = =) {New Thread (new MyThread2 (), "AAA"). Start ();      }} System.out.println ("------------------------------->"); New Thread (New Runnable () {public void run () {for (int i = 0; I <200; i++) {System.out.println ("anonymous object-->     ; "+ i);  }}}, "Ooxx"). Start (); } }

Package thread.ticket;

/** * Compare the differences between the two ways: * * Using inheritance, resources can not be shared;  * * Inheritance mode, after inheriting the thread class, you can no longer inherit other classes; * * * from the simple nature of the code, to inherit the way simple * * * Recommended interface method, convenient to expand later; resource sharing * * * *

Class Ticket2 extends Object implements runnable{

int num = 50; @Override public void Run () {for (int i = 0; i < i++) {if (num >0) {System.out.println (Thread.curre    Ntthread (). GetName () + "sell first" +num--+ "Zhang");   }}}} public class TicketDemo2 {public static void main (string[] args) {Runnable target = new Ticket2 ();   New Thread (Target, "A"). Start ();   New Thread (Target, "B"). Start ();  New Thread (Target, "C"). Start (); } }


The thread is opened by calling the Start method. Inherit the thread class
Subclasses overwrite the Run method in the parent class and store the code running by the thread in run.
The same thread that creates the subclass object is also created.
The thread is opened by calling the Start method.

Class MyThread extends thread{public MyThread (String name) {super (name); }

 public void Run () {  //thread body   for (int i = 0; i <; i++) {   //setname ("Sp Ringbrother ");    system.out.println (Super.getname () + i);   }  }  } public class ThreadDemo1 { public static void main (string[] args) {  for ( int i = 0; I < 200; i++) {   //system.out.println (This.getname () + i)    /*     * static Thread CurrentThread ()               returns a reference to the currently executing thread object.     * * */       //thread.currentthread (). SetName ("Ooxx");        string name = Thread.CurrentThread (). GetName (); &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (name+ "------>" +i);    if (i = =) {    //new MyThread (). Run ();//This does not represent a startup thread, just a method of invoking an object           new MyThread ("Sister Feng"). StarT ();    }   }     }

Package thread.ticket;

Class Ticket1 extends thread{int num = 50;  Public Ticket1 (String name) {super (name); public void Run () {for (int i = 0; i < i++) {if (num >0) {System.out.println (GetName () + "sell first" +    num--+ "Zhang");   }}}} public class Ticketdemo {public static void main (string[] args) {//3 windows Buy new Ticket1 ("a"). Start ();   New Ticket1 ("B"). Start ();  New Ticket1 ("C"). Start (); } }

Threads and processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.