staticclassThreadLocalMap{ privatevoidset(ThreadLocal<?> key, Object value){ Entry[] tab = table; int len = tab.length; int i = key.threadLocalHashCode & (len-1);
for (Entry e = tab[i]; e != null; // 解决hash冲突 如果当前位置已经有对象,那么看下一个位置 e = tab[i = nextIndex(i, len)]) { ThreadLocal<?> k = e.get();
publicclassThreadimplementsRunnable{ /* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null;
/* * InheritableThreadLocal values pertaining to this thread. This map is * maintained by the InheritableThreadLocal class. */ ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; privatevoidinit(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc, boolean inheritThreadLocals){ // ...... 省略其它代码 // 这里处理 InheritableThreadLocal if (inheritThreadLocals && parent.inheritableThreadLocals != null) this.inheritableThreadLocals = // 继承主线程的ThreadLocal ThreadLocal.createInheritedMap(parent.inheritableThreadLocals); // ...... 省略其它代码 } }
# 添加硬盘 注意 添加硬盘后需要重启才能识别设备 # 查看硬盘名称 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk # 原硬盘 ├─sda1 8:1 0 200M 0 part /boot/efi ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 18.8G 0 part ├─centos-root 253:0 0 16.8G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 1G 0 disk # 新加的硬盘 sr0 11:0 1 973M 0 rom # 硬盘分区 [root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x5ec44374.
Command (m for help): m # 帮助 Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition g create a new empty GPT partition table G create an IRIX (SGI) partition table l list known partition types m print this menu n add a new partition # 分区数量 o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit # 写入操作并退出 x extra functionality (experts only)
Command (m for help): n # 分区数量 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p # 分区数量 # 省略 ......
Command (m for help): w # 写入操作并退出 The partition table has been altered!
Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 200M 0 part /boot/efi ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 18.8G 0 part ├─centos-root 253:0 0 16.8G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 1G 0 disk └─sdb1 8:17 0 1023M 0 part sr0 11:0 1 973M 0 rom ## 分区格式化 注意地址是/dev/sdb1 [root@localhost ~]# mkfs -t ext4 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 261888 blocks 13094 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376
Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done
[root@localhost ~]# lsblk -f NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 vfat BAA0-0888 /boot/efi ├─sda2 xfs 90310ed0-d336-4f5e-9341-33c06c13c23b /boot └─sda3 LVM2_me Px4QV7-dZba-mIm7-fcam-jtdq-jZtK-qxHLyC ├─centos-root xfs 38446ac0-2dad-4f72-bffd-a1db0a419482 / └─centos-swap swap 2853a0af-5e55-497c-ad18-39354cfab4e5 [SWAP] sdb ext4 cf92cda9-9d49-4434-8494-c698876037b0 └─sdb1 ext4 0a1596cd-a209-41c8-bb73-26e9c0636fec sr0 iso9660 CentOS 7 x86_64 2020-11-03-14-55-29-00 # 查看分区情况 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 200M 0 part /boot/efi ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 18.8G 0 part ├─centos-root 253:0 0 16.8G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 1G 0 disk └─sdb1 8:17 0 1023M 0 part sr0 11:0 1 973M 0 rom ## 创建新文件夹 [root@localhost ~]# cd / [root@localhost /]# mkdir newdisk ## 挂载分区 [root@localhost /]# mount /dev/sdb1 /newdisk/ ## 查看挂载结果 [root@localhost /]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 200M 0 part /boot/efi ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 18.8G 0 part ├─centos-root 253:0 0 16.8G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 1G 0 disk └─sdb1 8:17 0 1023M 0 part /newdisk sr0 11:0 1 973M 0 rom
永久挂载
修改/etc/fstan文件
执行mount -a`生效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[root@localhost ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Thu Jul 21 06:03:22 2022 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # 增加挂载信息 # UUID 即为lsblk -f 下对应的uuid 也可以直接使用磁盘名(lsblk 结果中的Name)代替 /dev/sdb1 /newdisk ext4 defaults 0 0 /dev/mapper/centos-root / xfs defaults 0 0 UUID=90310ed0-d336-4f5e-9341-33c06c13c23b /boot xfs defaults 0 0 UUID=BAA0-0888 /boot/efi vfat umask=0077,shortname=winnt 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
# 查看/目录下一级目录大小 # du -h --max-depth=1 -c / 122M /boot 0 /dev du: cannot access ‘/proc/9713/task/9713/fd/4’: No such file or directory du: cannot access ‘/proc/9713/task/9713/fdinfo/4’: No such file or directory du: cannot access ‘/proc/9713/fd/3’: No such file or directory du: cannot access ‘/proc/9713/fdinfo/3’: No such file or directory 0 /proc 21M /run 0 /sys 36M /etc 28K /root 644M /var 8.0K /tmp 1.8G /usr 0 /home 0 /media 0 /mnt 56M /opt 0 /srv 20K /newdisk 2.6G / 2.6G total # 汇总
相关示例
1 2 3 4 5 6 7 8 9 10 11
# 统计/opt目录下文件数量 ls -al /opt |grep "^-"|wc -l # 统计/opt目录下目录数量 ls -al /opt |grep "^d"|wc -l # 统计/opt目录下全部文件数量 包含子文件夹 -R 递归获取 ls -alR /opt |grep "^-"|wc -l # 以树状结构展示/opt目录 tree /opt
#!/bin/bash # 顺便测试$* 和 $@ 的区别 # 这里很奇怪 如果我直接用 for i in $* 此时也是独立输出的和$@没有区别 for i in "$*" do echo "num is $i" done echo "============b ==============" for i in "$@" do echo "num is $i" done # 计算1-100的和 SUM=0 for (( i=0; i<=100; i++ )) do SUM=$[$SUM+$i] done echo "sum=$SUM"
while循环
1 2 3 4 5 6 7 8 9 10
#!/bin/bash # 从命令行输入一个数n,统计从1+2+..+n的值是多少? i=0 SUM=0 while [ $i -lt $1 ] do SUM=$[$SUM+$i] i=$[$i+1] done echo "结果为:$SUM"
# 替换第一个`Jack`为Mark sed '/s/Jack/Mark' fin.txt > hello Jobs hello Pony hello Mark, hi Jack
# 替换所有`Jack`为Mark sed '/s/Jack/Mark/g' fin.txt > hello Jobs hello Pony hello Mark, hi Mark # 替换第2、3行`Jack`为Mark sed '2,3s/Jack/Mark/g' fin.txt > hello Jobs hello Pony hello Mark, hi Mark # 找出包含字符 Pony 的那些行,将这些行中的 hello 替换成 hey sed '/Pony/s/hello/hey/g' fin.txt >> hello Jobs hey Pony hello Jack, hi Jack
# 修改第一个`Jack`为Mark sed -i '/s/Jack/Mark' fin.txt > hello Jobs hello Pony hello Mark, hi Jack # 在第一行前面插入 welcome sed -i '1i\welcome' fin.txt >> welcome hello Jobs hello Pony hello Jack, hi Jack
# 在第一行后面添加 welcome sed -i '1a\welcome' fin.txt
# 在匹配行的前面或者后面添加一行 sed -i '/Pony/a\welcome' fin.txt >> hello Jobs hello Pony welcome hello Jack, hi Jack
# 在匹配行前面插入welcome sed -i '/Pony/i\welcome' fin.txt
/** * The condition can only match the bean definitions that have been processed by the application context so far and, as such, it is strongly recommended to use this condition on auto-configuration classes only. If a candidate bean may be created by another auto-configuration, make sure that the one using this condition runs after. */ @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnBeanCondition.class) public@interface ConditionalOnBean {}
Given the head of a singly linked list, reverse the list, and return the reversed list.
指针解法
1 2 3 4 5 6 7 8 9 10 11 12
funcreverseList(head *ListNode) *ListNode { var prev *ListNode curr := head var last *ListNode for curr != nil { last = curr.Next curr.Next = prev prev = curr curr = last } return prev }
递归解法
1 2 3 4 5 6 7 8 9
funcreverseList(head *ListNode) *ListNode { if head == nil || head.Next == nil{ return head } last := reverseList(head.Next) head.Next.Next = head head.Next = nil return last }
反转链表前N个元素
在反转整个链表的基础上增加条件即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcreverseNth(head *ListNode, n int) *ListNode { var prev *ListNode = nil curr := head var last *ListNode for curr != nil && n > 0 { last = curr.Next curr.Next = prev prev = curr curr = last n-- } // 连结 head.Next = curr return prev }