What is the difference between arraylist and rule list? Provides examples to illustrate the functions that can be implemented by the shortlist.

Source: Internet
Author: User

For processing a column of data items, Java provides two classes: arraylist and arraylist. The internal implementation of arraylist is based on the internal array object []. So Conceptually, it is more like an array, however, the internal implementation of The shortlist is based on a set of connected records. Therefore, it is more like a linked list structure. Therefore, they differ greatly in performance.

When inserting data in front of or in the middle of the arraylist, all the data after it must be moved back. This will inevitably take a lot of time. Therefore, when you add data after a column of data instead of adding data in front or in the middle and randomly access the elements in the column, using arraylist provides better performance;

When accessing an element in the linked list, you must start from the end of the linked list and search for the element one by one in the connection direction until you find the required element. Therefore, when you add or delete data in front or middle of a column of data and access the elements in the column in sequence, you should use the sort list.

In programming, the two situations alternate. In this case, you can consider using a general interface such as list, instead of concerning the specific implementation. In specific circumstances, its performance is guaranteed by the specific implementation.

Case: Implementation stack of the consumer list

Case Study

Arraylist has a relatively high query efficiency and a relatively low efficiency of adding and deleting actions. It is suitable for a set of Element Management Functions with frequent queries and fewer addition and deletion actions. The query efficiency of the shortlist is low, but the addition and deletion efficiency is high. It is applicable to Element Management sets that are frequently added or deleted and have a small number of queries.

Both arraylist and worker list are thread-insecure.

Implement stack 1) array (arraylist, Which is inefficient at adding, deleting, and not suitable)

2) Implement list (a good way to implement stack)

3) Java. util. stack class: Stack is a subclass of vector. The Vector class is a thread-safe (a heavyweight class) and inherits the vector method. The verctor class and arraylist have almost the same functions. (Stack class is not recommended for stack implementation ).

Implementation Process

Import java. util .*;

Public class teststack {

Public static void main (string [] ARGs ){

Mystack MS = new mystack ();

Ms. Push ("chenzq ");

Ms. Push ("liucy ");

Ms. Push ("Bailu ");

System. Out. println (Ms. Pop ());

System. Out. println (Ms. Pop ());

System. Out. println (Ms. Pop ());

}

}

Class mystack {

Private writable list LL = new writable list ();

Public void push (Object O ){

Ll. addfirst (O );

}

Public object POP (){

Return LL. removefirst ();

}

}

32. What is the concept of thread? How do threads communicate with each other?

A thread is an execution unit in a process. Each thread in a process corresponds to a set of CPU commands, a set of CPU registers, and a stack. The process has a dynamic meaning, but it is actually reflected by the thread. In this sense, the dynamic meaning of the Process in Windows is no longer obvious, it can only be regarded as setting a scope for the resources occupied by the program. What is truly dynamic is the thread.

In fact, Java provides three very important methods to skillfully solve inter-thread communication problems. The three methods are: Wait (), Policy (), and policyall (). They are the final methods of the object class, so each class has them by default.

Although all classes have these three methods by default, they are only within the scope of the synchronized keyword, in addition, it is of practical significance only when these three methods are used in the same synchronization problem.

The syntax format declared by these methods in the object class is as follows:

Final void wait () throws interruptedexceptionfinal void Policy () final void policyall ()

The wait () method can be called to allow the thread that calls this method to release the lock for shared resources, and then exit from the running state and enter the waiting queue until it is awakened again. Calling the Y () method can wake up the first thread in the waiting queue waiting for the same shared resource, and cause the thread to exit the waiting queue and enter the runnable state. Call the yyall () method to exit all threads waiting for the same resource in the queue from the waiting state and enter the runable state. At this time, the thread with the highest priority is the first to execute. Obviously, using these methods does not have to recycle the status of shared resources, but simply wake up the threads in the waiting queue as needed. This not only saves valuable CPU resources, but also improves program efficiency.

Because the wait () method is declared as throwing an interruptedexception exception during the Declaration, when calling the wait () method, you need to put it in try... Catch code block. In addition, you also need to put this method into a synchronization code segment. Otherwise, the following exception occurs:

"Java. Lang. illegalmonitorstateexception: current thread not owner"

Can these methods implement inter-thread communication? The following describes how to solve the communication problem between multiple threads through a program through the multi-thread synchronization model: producer and consumer problems.

Case: thread Communication

Case Study

The following program demonstrates the specific implementation process of communication between multiple threads. Four classes are used in the program. The metadata data class is used to define shared data and synchronization methods. The wait () and notify () methods are called in the synchronous method, and a semaphore is used to transmit messages between threads.

Implementation Process

// Communicationdemo. Java Description: The message passing process between the producer and consumer.

Class metadata data

{

Private char C;

Private Boolean isproduced = false; // semaphore

Public synchronized void put1_char (char c) // synchronous method put1_char ()

{

If (isproduced) // if the product has not been consumed, the producer waits

{

Try {Wait (); // producer waits

} Catch (interruptedexception e ){

E. printstacktrace ();

}

}

This. C = C;

Isproduced = true; // mark that it has been produced

Y (); // notify the consumer that it has been produced and can be consumed

}

Public synchronized char get1_char () // synchronous method get1_char ()

{

If (! Isproduced) // if the product is not yet produced, the consumer waits

{

Try

{

Wait (); // wait for the consumer

} Catch (interruptedexception e ){

E. printstacktrace ();

}

}

Isproduced = false; // indicates that the message has been consumed.

Y (); // notifications need to be produced

Return this. C;

}

}

Class producer extends thread // producer thread

{

Private encrypted data S;

Producer (sharedata S)

{

This. S = s;

}

Public void run ()

{

For (char CH = ''a'; ch <= ''d'; ch ++)

{

Try

{

Thread. Sleep (INT) (math. Random () * 3000 ));

} Catch (interruptedexception e ){

E. printstacktrace ();

}

S. put1_char (CH); // store the product

System. Out. println (CH + "is produced by producer .");

}

}

}

Class consumer extends thread // consumer thread

{

Private encrypted data S;

Consumer (consumer data S)

{

This. S = s;

}

Public void run ()

{

Char ch;

Do {

Try

{

Thread. Sleep (INT) (math. Random () * 3000 ));

} Catch (interruptedexception e ){

E. printstacktrace ();

}

Ch = S. get1_char (); // retrieve the product from the Repository

System. Out. println (CH + "is consumed by consumer .");

} While (Ch! = ''D '');

}

}

Class communicationdemo

{

Public static void main (string [] ARGs)

{

Response Data S = new response data ();

New consumer (s). Start ();

New producer (s). Start ();

}

}

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.