Java Basic pen Questions (iii)
What is Threadlocal,threadlocal's role?
As explained in the JDK documentation: the role of threadlocal is to provide local variables within a thread that, when accessed in a multithreaded environment, can guarantee the independence of variables in each thread.
It can be seen from here that the purpose of introducing threadlocal is to provide local variables within the thread, rather than to solve the problem of multithreaded access to shared objects. In fact, threadlocal cannot solve the problem of multithreaded access to shared objects at all.
you can hold a number of threadlocal objects in each thread, which are stored in the thread's threadlocalmap after the hash, where the key is the Threadlocal object, and value is a copy of the object in this thread. The threadlocal itself does not store value values, but only as key in Threadlocalmap index value
The key in each thread containing threadlocalmap is a weak reference to the threadlocal variable, and if a threadlocal variable does not have an external strong reference to reference it, it will be garbage collected the next time the JVM is GC, The value of key is NULL in the map, and this value cannot be accessed. If the current thread is no longer closed (for example, the current thread is in a thread pool), the object that value points to may never be released, or it cannot be reclaimed, causing a memory leak.
Threadlocalmap's designers obviously thought of this problem, so in every operation on Threadlocalmap Set,get,remove, the entry of key in map was cleared. As a result, threadlocal generally do not have a memory leak risk.
However, it is not a wise choice to give the work of purging null objects, so smart you, after using the Threadlocal object in thread, must remember to call the Threadlocal Remove method for manual removal.
Sample code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28
Output results
Thread-0 start, local = 0
Thread-2 start, local = 0
Thread-1 start, local = 0
Thread-2 start, local =
T hread-3 start, local = 0
Thread-0 start, local = 0
Thread-3 start, local =
Thread-1 start, local = 6
1 2 3 4 5 6 7 8
common commands for Linux 1. command format and directory processing command
Ls–a "View hidden file"
ls–l view file information long format display "
ls–d" view the details of the specified directory "
ls–h" show capacity Size "
ls–i" View any File I node "
ll filename" View more information for a file "
2. Directory Processing Command
Mkdir–p "Recursively create directory"
CD "Toggle Specify Directory"
pwd "show current directory"
rmdir "Delete empty directory"
cp "copy file to a directory"
cp–r "replication directory
CP –p "Preserve file properties"
mv "cut file, rename"
rm "Delete file"
rm–r "Delete directory"
rm–f "enforce"
3. File Processing command
Touch "Create empty file"
Cat View File contents "
cat–n" display line number "
Tac" Display file contents (reverse listing) "More
" page display file contents (space or f[page), enter[line Wrap], q or q[exit ] "
Less" page shows the contents of the file (can page up), fill out/searchable keywords, n equals next,pageup and PageDown "head" to display the first
few lines of the file "
head-n" specified number of lines, Head–n 20/ Etc/services "Tail" displays the following lines
of "
tail–n" to specify the number of lines "
tail–f dynamic Display end of file"
4. Link command
LN "generates a linked file, similar to a copy, that can be synchronized to update"
ln–s "to create soft links, equivalent to shortcuts"
5. Network Command
Write username "Send information to online users to ctrl+d save End"
Wall Information "Send broadcast information"
PING-C 4 IP Address "Test network connectivity,-C: Specify 4 times to send"
Ifconfig eth0 192.168.0.169 "View and set network card information"
Mail username "View send email to ctrl+d save end, help view information"
Last "list user information for current and past login systems"
Lastlog-u 502 "Check the last time a specific user logged in"
Traceroute url "Display packets to the host between the path IP and so on"
Netstat-t/-u/-l/-r/-n "Display network-related information,-T:TCP protocol,-U:UDP protocol,-L: Listening,-r: Routing,-n: Displaying IP addresses and port numbers"
Netstat-tlun "View port on native monitor"
Netstat-an "View all networks on this computer"
Netstat-rn "View native routing table"
Mount "Mount command-Find out more about Baidu"
mount/dev/sr0/mnt/cdrom/"Mount CD"
Umount/dev/sr0 "Unload mount point" 6. User Management Commands
Useradd User Name "Add new user"
passwd user name "Set User password"
Who "View login user information, TTY local login, PTS remote Terminal"
W "View the details of the logged-in user, all" 7. Compression decompression Command
Gzip Files compressed files, compressed format:. gz "
Gunzip file" uncompressed. GZ compressed Files "
tar-zcf temp.tar.gz Temp" Packaging directory,-C: Packaging,-V: Display details, -F: Specify file name,-Z: Package compression at the same time, temp: filename "
tar-zxf temp.tar.gz" Extract directory,-x: Unpack,-V: Show Details,-f: Specify filename,-Z: Unpack at the same time, temp: file name " &NBSP
Zip-r Temp.zip Temp "compression price or directory,-r: Compressed directory, temp: filename"
Unzip temp "unzip. zip file, temp: file name or directory"  
Bzip2-k Temp "Compressed file, K: Create a compressed file to keep the original file, temp: filename"
Bunzip2-k Temp "Extract the file, K: Extract the original file, temp: file name"  
TAR-CJF temp.tar.bz2 temp "compress"
TAR-XJF temp.tar.bz2 "extract" 8. Network Command
Write username "Send information to online users to ctrl+d save End"
Wall Information "Send broadcast information"
PING-C 4 IP Address "Test network connectivity,-C: Specify 4 times to send"
Ifconfig eth0 192.168.0.169 "View and set network card information"
Mail username "View send email to ctrl+d save end, help view information"
Last "list user information for current and past login systems"
Lastlog-u 502 "Check the last time a specific user logged in"
Traceroute url "Display packets to the host between the path IP and so on"
Netstat-t/-u/-l/-r/-n "Display network-related information,-T:TCP protocol,-U:UDP protocol,-L: Listening,-r: Routing,-n: Displaying IP addresses and port numbers"
Netstat-tlun "View port on native monitor"
Netstat-an "View all networks on this computer"
Netstat-rn "View native routing table"
Mount "Mount command-Find out more about Baidu"
mount/dev/sr0/mnt/cdrom/"Mount CD"
Umount/dev/sr0 "Unload mount point" vim command
1). Vim Common operation
Vim filename "Enter or new-enter command mode"
i/a/o "into insert mode, a: Insert at the cursor's end-of-file, a: Insert at the end of the line where the cursor is, I: Insert at the beginning of the cursor, I: Insert the line at the start of the cursor, O: Insert a new line under the cursor, O: Insert a new line on the cursor
: Set Nu "Add line number"
: Set Nonu "Cancel line number"
GG "to the first line"
G "to the last line"
NG "to Nth line"
: n "to Nth line--– recommended"
$ "Move to end of line"
0 "Move to Beginning"
X "Delete character ——— at cursor-recommended"
NX "Delete the n characters at the back of the cursor"
DD "Delete cursor row, ndd delete n line--– recommended"
DG "Delete cursor line to end of file"
D "Delete the cursor at the end of the line"
: n,nd "Delete specified range of rows"
yy "Copy Current line ———— recommended"
Nyy "Copy current line n rows"
DD "Cut Current Line"
Ndd "Cut n rows below current line"
p/p "Paste at the current cursor line (p) or line (p)--– recommended"
R "replaces the character where the cursor is located"
R "Replace the character at the cursor, press ESC to end"
U "Cancel the previous operation, undo ———-Recommendation"
/string search for the specified string, string: String, N: Search for the next occurrence of the specified string
: Set IC "Ignore case when searching"
: Set Noic Ignore case while canceling search
:%s/old/new/g "Full text substitution specified string, colon refers to full text substitution,%s: full-Text Search, old: string to replace, new: Replacement of the String, G: Do not ask for confirmation"
: N,NS/OLD/NEW/C replaces the specified string in a range (N,ns: Starting line, terminating line s), C: asking for confirmation "
: w "Save changes--recommended"
: W/tmp/temp.txt "Save as specified file"
: Wq "Save exit ——— recommended"
: q! "Do not save Exit"
ZZ "shortcut keys, saving changes and exiting"
: wq! "Save changes and exits (file owner and root can be used)"
Press ESC "exit mode, enter command mode"
2). Vim Use skill
: R/tmp/temp.txt (imported file) "Import temp.txt contents to Cursor"
: R!date (command) "You can perform the appropriate action command without exiting vim, Date: System time Command"
: Map ^p i# "define shortcut Keys: map,^p: Press ctrl+v+p (shortcut key press ctrl+p) to comment on the line"
: Map ^b 0x "define shortcut key: Map,^b: Press ctrl+v+b (shortcut key Press ctrl+b), uncomment the line"
: 1,4s/^/#//g "Multiline comment, adding 1 to 4 lines at the beginning of the line #"
: 1,4s/^#//g "Cancel multiline comments, and remove the # 1 to 4 lines from the beginning of the line."
: 1,4s/^/\/\//g "Multiline comment, adding 1 to 4 lines at the beginning of the line//"
: 1,4s/^\/\//g "Cancel Multiline comment, remove 1 to 4 lines from the beginning of the line"
: AB mymail www.xxx@163.com "Replace, fill in the MyMail when the edit is entered into the mailbox"
The shortcut key or edit mode command will fail after reboot and must be kept permanently in the/ROOT/.VIMRC or/HOME/*/.VIMRC directory-NOTE: No need to fill in
More commonly used command reference http://blog.csdn.net/hezemin0315/article/details/50666803 to take out data in a department that is above the department's average wage
Select emp.* from EMP (select Deptno,avg (sal) sal from emp GROUP by Deptno) as Emp1
where Emp.deptno =EMP1.DEP TNO and Emp.sal>emp1.sal
1 2
briefly describe the role of database indexing. function
: This is because creating an index can greatly improve the performance of your system.
First, you can guarantee the uniqueness of each row of data in a database table by creating a unique index.
Second, you can greatly speed up the retrieval of data, which is the main reason to create indexes.
Third, you can speed up the connection between tables and tables, especially in terms of realizing the referential integrity of the data.
Finally, when you use grouping and sorting clauses for data retrieval, you can also significantly reduce the time to group and sort in a query.
In the process of querying, the optimization of the hidden device can be used to improve the performance of the system by using the index. Add: (1) The disadvantage of creating an index.
First, it takes time to create indexes and maintain indexes, which increase as the amount of data increases.
Second, the index needs to occupy the physical space, in addition to the data table occupies the data space, each index also occupies a certain physical space, if you want to establish a clustered index, then need more space.
Third, when the data in the table is added, deleted and modified, the index will be maintained dynamically, thus reducing the data maintenance speed. (2) The index is built on top of some columns in the database table.
Therefore, when you create an index, you should carefully consider which columns you can create indexes on, and on which columns you cannot create indexes. In general, you should create indexes on these columns, such as: &NBSP
① on which columns you can create an index
You can speed up your search on columns that you frequently need to search,
on columns that are primary keys, Enforces the uniqueness of the column and arranges the structure of the data in the organization table;
in frequently used on connected columns, these columns are mostly foreign keys, you can speed up the connection;
Create indexes on columns that often need to be searched by scope because the indexes are already sorted, The specified range is continuous;
creates an index on a column that is often sorted, because the index is sorted so that the query can take advantage of the sort of the index, speeding up the sorting query time;
to speed up the criteria by creating indexes on the columns that are often used in the WHERE clause. &NBSP
② which columns cannot create an index
Similarly, for some columns, you should not create indexes. In general, these columns that should not be indexed have the following characteristics:
First, you should not create indexes for columns that are rarely used or referenced in queries. This is because, since these columns are rarely used, they are indexed or indexed and do not increase the query speed. On the contrary, because of the addition of indexes, it reduces the maintenance speed of the system and increases the space requirement. &NBSP
Second, you should not add indexes to columns that have very few data values. This is because, because of the low values of these columns, such as the gender column of the personnel table, the result set's data rows account for a large proportion of the data rows in the table, that is, the data rows that need to be searched in the table are large. Adding indexes does not significantly speed up the retrieval. &NBSP
Third, columns that are defined as text, image, and bit data types should not be indexed. This is because these columns have either a large amount of data or a very small number of values. &NBSP
Four, the index should not be created when the modification performance is far greater than the retrieval performance. This is because the modification performance and retrieval performance are contradictory. When indexing is added, retrieval performance is improved, but modification performance is reduced. When the index is reduced, the modification performance is improved and the retrieval performance is reduced. Therefore, indexes should not be created when the modification performance is far greater than the retrieval performance. briefly describe the difference between an abstract class and an interface
A: ① abstract class is the abstraction of things (class abstract), is a template design, and interface is an abstraction of behavior, is a code of conduct, is a kind of radiation design;
② Abstract classes can define both abstract methods and Non-abstract methods, and only abstract methods can be defined in interfaces;
③ Abstract classes can have member variables (containing static member variables), properties, constants, and they can be non-public, while the member variables in an interface can only be public and final, and properties can only be common;
④ abstract classes can have static code blocks and static methods, while interfaces cannot contain static blocks of code and static methods;
⑤ A class can inherit only one abstract class, while a class may implement multiple interfaces; count the number of characters in a string
public static void Count (String str) {
map<string,integer> map=new hashmap<string,integer> ();
for (char ch:str.getChars ()) {
String ss=string.valueof (CH);
Integer Count=map.get (ss);
if (count==null)
count=1;
Map.put (Ss,count);
}
For (String St:map.keys ()) {
System.out.println (st+ ":" +map.get (ST));}
1 2 3 4 5 6 7 8 9 10 11-12
Enter a positive integer, output in reverse order
public class Examinationone {public
static void Main (string[] args) {
examinationone test=new examinationone ( );
System.