君子兰槐的博客

不怨天不尤人。知行合一。致良知。

0%

In the ‘Standard Delegates’ section of the Delegation Properties page, there is one sentence says:

If you want thread safety, use blockingLazy() instead: it guarantees that the values will be computed only in one thread and that all threads will see the same value.

But I searched the kotlin standard library and can’t find the blockingLazy() method.

I was using the kotlin 1.9.10:

$ kotlin -version
Kotlin version 1.9.10-release-459 (JRE 17.0.8+9-LTS-211)

and kotlin standard library 1.9.0:

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>1.9.0</version>
</dependency>

It seems that kotlin has removed the blockingLazy method.

I did find a lazy method which allow passing a lock parameter. It should be the new way to use a blocking lazy.

So I implement a thread-safe version here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ThreadSafeLazyExample {
init {
println("Created!")
}

private val lazyLock: Int = 1

// a thread safe lazy
val lazyStr: String by lazy(lazyLock) {
println("computed!")
"my lazy"
}
}

fun main() {
val sample = ThreadSafeLazyExample()
println("lazyStr is ${sample.lazyStr}")
println(" =${sample.lazyStr}")
}

近期无法通过git从github克隆项目的解决办法

问题描述

前些天,同事问我:“你能从github上克隆代码吗?”我的第一感觉是他的网络有问题。但是当我在自己的电脑上测试了之后
才发现,真是这样。

直到今天,我要往github上推送我的笔记(我的笔记托管在github上),git push命令提示:

ssh: connect to host github.com port 22: Operation timed out

网上一通搜索,从一位开发者那里得知,我们伟大的GFW从1月16号开始和谐了github的默认ip地址。

解决办法

那么,该怎么办呢?

办法总是有的。原来github除了在22端口上提供ssh服务,在另外一个域名ssh.github.com的443端口也提供ssh服务。

打开~/.ssh/config文件:

Host github.com
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_rsa

把HostName改为ssh.github.com, 然后增加一行Port: 433,保存。修改后的文件如下:

Host github.com
    HostName ssh.github.com
    User git
    Port 443
    PreferredAuthentications publickey
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_rsa

这样,我们就又能愉快地访问github了。

行是知之始,知是行之成。 —— 王阳明