The use of Java collections and Date and io stream multithreading! ! ! Download files in multiple threads

The use of Date class! !

Insert picture description here
Collection
General overview
List, set, Map are all interfaces, the first two are inherited from Collection interface, Map is independent
set under HashSet, LinkedHashSet, TreeSet
List under ArrayList, Vector, LinkedList
Map under Hashtable, LinkedHashMap, HashMap ,
There is also a Queue interface under the TreeMap Collection interface, with PriorityQuece class

Collection interface
List is ordered and can be repeated

1
Advantages of AyyayList : low-level data structure, fast query, slow addition and deletion
Disadvantages: unsafe thread, high efficiency
. !
Insert picture description here

2. Vector
advantages: the underlying data structure is an array, the query is fast, and the increase and deletion are slow.
Disadvantages: thread safety, low efficiency

3 LinkedList
advantages: the underlying data is a linked list, the query is slow, adding and deleting are relatively fast
Disadvantages: thread is not safe, high efficiency
arraylink and LinkedList
Insert picture description here

set is unordered, the only thing will be automatically deduplicated

The
underlying data structure of HashSet is a hash table (unordered, unique).
How to ensure the uniqueness of elements?

Rely on two methods: hashCode() and equals()

LinkHashSet

The underlying data structure is linked list and hash table

1 The order of the elements is guaranteed by the chain
2 The uniqueness is guaranteed by the hash table

The
underlying data structure of TreeSet is a red-black tree (unique, ordered).
How to ensure the sorting of elements?
Natural sorting How does the
comparator sorting
2 guarantee the uniqueness of elements?
Determine whether the return value of the comparison is 0

The main function of TreeSet is used to sort
LinkedHashSet. The main function of LinkedHashSet is to ensure that FIFO is an ordered set (first in, first out).
HashSet is just a general set of stored data.

Case study

Insert picture description here
Insert picture description here

The Map interface map is saved by key and value. If the key is the same, it will be automatically removed.
There are three implementation classes HashMap, TreeMap, and HashTable

TreeMap is ordered, the other is disordered
Hashtable method is synchronized, HashMap method is not synchronized, the main difference between the two

So Hashtable is thread-safe, HashMap is not thread-safe.
HashMap is more efficient, while Hashtable is less efficient. *
io stream *
stream: represents any data source object capable of generating data or a receiving end object capable of accepting data.
The essence of the stream: data transmission. According to the characteristics of data transmission, the stream is abstracted into various categories, which is convenient and intuitive Data manipulation.

Role: Establish a transmission channel for the data source and destination

The classification of IO stream
has input stream, output stream

Byte stream InputSteram outputStream

Insert picture description here
Character stream Reader Writer

Insert picture description here
Multi-threaded notes
What is a process: refers to an application running in memory, each process has an independent memory space, an application can run multiple processes at the same time, the process is also the one-time execution process of the program, which is the system operation The basic unit of the program, the system runs a program is the process of a process from creation, operation to death

Thread: an independent execution unit within a process. A process can run multiple threads concurrently. It can be understood that a process is equivalent to a single cpu operating system, and threads are
how multiple tasks are running in the system The case of creating a thread
way one
Insert picture description here
way two
Insert picture description here
ticket

Insert picture description here
Multi-threaded downloading picture method one, (ps is simple)

Insert picture description here
The second
method is that each thread has a start and an end, so it is constructed with parameters.
Set the address
to be downloaded. Write and output streams to avoid synchronization of the three threads. Set a synchronization lock.
Insert picture description here
Test class
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46937429/article/details/108980247