水浅

  • Home

  • Tags14

  • Categories10

  • Search

notify 的使用

Posted on 2021-02-13
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
29
30
31
32
33
34
35
public class NotifyTest {
public synchronized void testWait(){
System.out.println(Thread.currentThread().getName() +" Start-----");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +" End-------");
}

public static void main(String[] args) throws InterruptedException {
final NotifyTest test = new NotifyTest();
// 创建五个线程取运行对象的方法
for(int i=0;i<5;i++) {
new Thread(new Runnable() {
@Override
public void run() {
test.testWait();
}
}).start();
}

//通知一个 线程
synchronized (test) {
test.notify();
}
Thread.sleep(3000);
System.out.println("-----------分割线-------------");
//通知所有线程
synchronized (test) {
test.notifyAll();
}
}
}

IO通道的使用

Posted on 2021-02-13
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
*
* @author devin
* @date 2021-01-07 15:52
* @version 1.0.2
*/
public class ChanTest {

private static FileOutputStream fileOutputStream;
private static FileChannel fileChannel;

public static <buffer, buffer1, buffer2> void main(String[] args) throws IOException, InterruptedException {
fileOutputStream = new FileOutputStream("D://out.txt");
fileChannel = fileOutputStream.getChannel();

for(int i = 0;i < 10;i++){
Thread thread1 = new Thread(){

@Override
public void run() {
ByteBuffer buffer1 = ByteBuffer.wrap("123\r\n".getBytes());
try {
fileChannel.write(buffer1);
} catch (IOException e) {
e.printStackTrace();
}
}
};

Thread thread2 = new Thread(){

@Override
public void run() {
ByteBuffer buffer2 = ByteBuffer.wrap("654\r\n".getBytes());
try {
fileChannel.write(buffer2);
} catch (IOException e) {
e.printStackTrace();
}
}


};
thread1.start();
thread2.start();


}
Thread.sleep(3000);
fileChannel.close();
fileOutputStream.close();
}
}

如何学习一门知识

Posted on 2021-02-13
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class BlockingQueueTest {
public static void main(String[] args) {
BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName() + "\t put A ");
blockingQueue.put("A");

System.out.println(Thread.currentThread().getName() + "\t put B ");
blockingQueue.put("B");

System.out.println(Thread.currentThread().getName() + "\t put C ");
blockingQueue.put("C");

} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t1").start();

new Thread(() -> {
try {

try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
blockingQueue.take();
System.out.println(Thread.currentThread().getName() + "\t take A ");

try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
blockingQueue.take();
System.out.println(Thread.currentThread().getName() + "\t take B ");

try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
blockingQueue.take();
System.out.println(Thread.currentThread().getName() + "\t take C ");

} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t2").start();
}
}

CAS 版本号应用

Posted on 2021-02-13
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
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
*
* @author devin
* @date 2021-01-08 22:11
* @version 1.0.2
*/
public class MyUnsafe {
private static AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<Integer>(100,1);

public static void main(String[] args) {
new Thread(() -> {
System.out.println("t1拿到的初始版本号:" + atomicStampedReference.getStamp());

//睡眠1秒,是为了让t2线程也拿到同样的初始版本号
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
atomicStampedReference.compareAndSet(100, 101,atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);
atomicStampedReference.compareAndSet(101, 100,atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);
},"t1").start();

new Thread(() -> {
//得到共享变量的版本号
int stamp = atomicStampedReference.getStamp();
System.out.println("t2拿到的初始版本号:" + stamp);

//睡眠3秒,是为了让t1线程完成ABA操作
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("最新版本号:" + atomicStampedReference.getStamp());
//确认当前值 修改值 确认版本号 赋值新的版本号 获得修改后的值 修改成功与否
System.out.println(atomicStampedReference.compareAndSet(100, 2019,3,atomicStampedReference.getStamp() + 1) + "\t当前 值:" + atomicStampedReference.getReference());
},"t2").start();
}

}

hexo使用

Posted on 2021-02-13 | Edited on 2021-02-15
  • 新建一篇文章

    1
    2
    3
    hexo new "博客名称"

    hexo new page --path about/me "About me"
  • 部署博客

    1
    2
    3
    4
    # 编译
    hexo g
    # 发布博客
    hexo d
  • 启动服务器

    1
    hexo server
  • 多分类

    1
    2
    3
    4
    5
    categories:
    - Diary
    tags:
    - PS3
    - Games
  • 分类和标签的使用规范

    1
    2
    分类:优先级 以领域划分   机器学习   生活   web开发  java系列
    标签:语言 读书笔记 代码 总结

java基础

Posted on 2021-01-28 | Edited on 2021-03-03 | In 面试

异常 接口和抽象类

  • 接口

    • 接口的变量是默认是public static final

    • 一个类可以实现多个接口

    • 一个类实现接口必须实现接口的所有方法 可以多继承

  • 抽象类

    • 抽象可以有自己的变量
    • 抽象类可以有自己的方法
    • 可以包含静态方法 单继承
    • 抽象的方法不需要全部实现 也就是适配器模式

异常

分为运行异常和检查异常

空指针异常 数组越界异常

IO异常 sql 异常

反射

解释:通过类名获得类的方法和属性,它的产生是满足动态编译。

反射有三种方式

forName

l类的class

对象的getClass

序列化

(1)Java序列化就是指把Java对象转换为字节序列的过程

​ Java反序列化就是指把字节序列恢复为Java对象的过程。

final finally finalize

final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承。

finally是异常处理语句结构的一部分,表示总是执行。

finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源

请解释hashCode()和equals()方法有什么联系?

相等的对象必须有相等的hashcode

有相同的hashcode 不一定相等 hash算法会有冲突

泛型擦除

1
2
3
4
5
6
public class Foo {
public void listMethod(List<String> stringList){
}
public void listMethod(List<Integer> intList) {
}
}

报方法签名重复错误

  • List、List 擦除后的类型为 List。

  • List、List[] 擦除后的类型为 List[]。

  • List<? extends E>、List<? super E> 擦除后的类型为 List。

  • List<T extends Serialzable & Cloneable> 擦除后类型为 List。

    Java 为什么这么处理呢?有以下两个原因:

避免 JVM 的大换血。如果 JVM 将泛型类型延续到运行期,那么到运行期时 JVM 就需要进行大量的重构工作了,提高了运行期的效率。
版本兼容。 在编译期擦除可以更好地支持原生类型(Raw Type)。

明白了 Java 泛型是类型擦除的,下面的问题就很好理解了:

无法声明泛型数组

1
2
3
4
5
6
7
// 这种报错
List<String>[] list = new List<String>[];

List<List<Integer>> list = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
list1.add(2);
list.add(list1);

泛型的理解

​ 要理解泛型首先要立即泛型擦除,java字节码不包含泛型类型信息,使用泛型的时候会变编译器擦除,这个过程就叫泛型擦除。泛型的附加类型JVM是不可见的。它和C++的模板类型是有区别的

  • 泛型是没有.class 对象

  • 声明不同的泛型实例 泛型类只会被加载一次

    • 1
      2
      3
      4
      ArrayList<String> list1 = new ArrayList<>();

      ArrayList<Integer> list2 = new ArrayList<>();
      //jvm 只会加载一次ArrayList
  • 泛型的参数类型不能用在java 处理异常中

###

java 死锁代码

Posted on 2020-12-30 | Edited on 2021-02-24 | In java系列
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

package jvm;

/**
* @author devin
* @version 1.0.2
* @date 2021-01-17 14:17
*/
import java.util.concurrent.TimeUnit;

/**
* 资源类
*/
class HoldLockThread implements Runnable{

private String lockA;
private String lockB;

// 持有自己的锁,还想得到别人的锁

public HoldLockThread(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}


@Override
public void run() {
synchronized (lockA) {
System.out.println(Thread.currentThread().getName() + "\t 自己持有" + lockA + "\t 尝试获取:" + lockB);

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}

synchronized (lockB) {
System.out.println(Thread.currentThread().getName() + "\t 自己持有" + lockB + "\t 尝试获取:" + lockA);
}
}
}
}

Es6 模块的导入和导出

Posted on 2020-10-30 | Edited on 2021-01-26 | In node.js

b.js

1
2
3
4
5
6
7
8
export function fun(){
console.log("fun1")
}

export let person = {
name:"devin",
age:12
}

c.js

1
2
3
4
5
6
7
8
let a = 12
let fun = function(){
console.log("function")
}

export default{
a,fun
}

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="module" >
// import * as util from './a.js';
// //import {fun, person} from "./a.js";
// console.log(util.fun(),util.person)
// import a from './exportDefault';
// a.show();
// console.log(a.a);

// import * as util from "./b.js"
import c from "./c.js"
// console.log(util.fun)
console.log(c.a)
c.fun()

</script>
</body>
</html>

微信小程序原生组件的坑

Posted on 2020-09-30 | Edited on 2021-02-14 | In 微信小程序
  • style
1
<scroll-view :style="{'height': '300px'}"></scroll-view>要设置高度
  • 原生的事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<scroll-view  :style="{'height': '300px'}" :scroll-y="true" @scrolltolower="scrolltolower" @scroll="scroll" >
<div :style="{'height': '200px','background-color':'red'}">tyuiolpo</div>
<div class="" :style="{'height': '200px','background-color':'red'}">dsdsd
</div><div class="" :style="{'height': '200px','background-color':'red'}">grytyju</div>
</scroll-view>

数据部分
methods: {
scrolltolower(){
console.log(7)
},
scroll(e) {
console.log(6)
console.log(e)
},
}

pandas基本操作

Posted on 2019-09-18 | Edited on 2020-04-20 | In python

1. pandas DataFrame对象的创建

1
2
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))

2. 操作数据

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
29
30
31
32
33
34
35
36
37
df.head()
df.tail(3)
df.index
df.describe()
# 单列索引
df.iloc[1:2,2:]
# 特定索引
df.iloc[[1,2],[2,3]]
# 布尔索引
df[df>0.5] = 1
# 赋值列
df2['E'] = ['A,','B','C','E','3','3']
# 赋值行
df2.loc['e'] = [1,2,3,4,5]
#访问某个位置
df2.iat[0,1]
#删除带有缺失值的行
df1.dropna(how = 'any')
#填充空数据
df1.fillna(value=5)
# 获得每一列的平均值、
df1.mean()
# 获得每一行的平均值
df1.mean(1)
# 修改行标签
df1.index = ['A','B','C']
#修改行列
data.rename(index={'A':'D', 'B':'E', 'C':'F'}, columns={'a':'d','b':'e','c':'f'}, inplace = True)
#连接两个表
pe = [df1,df2]
pd.concat(pe)
#数据库连接风格
left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})
right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
pd.merge(left, right, on='key')
#写入文件
df1.to_csv('foo.csv')
1…34
Author

Author

40 posts
10 categories
14 tags
GitHub 简书
Links
  • csdn
  • github
© 2021 认识世界 认识你自己