kotlin协程和java线程池的组合尝试

说明

初学kotlin,这是本人对协程+线程池尝试
目前已发现的问题是,协程无法保活(main线程运行结束后,未执行完的协程会停止执行)

pom片断

    <properties>
        <kotlin.version>1.3.31kotlin.version>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlingroupId>
            <artifactId>kotlin-reflectartifactId>
            <version>${kotlin.version}version>
        dependency>
        <dependency>
            <groupId>org.jetbrains.kotlinxgroupId>
            <artifactId>kotlinx-coroutines-coreartifactId>
            <version>1.3.0-M1version>
        dependency>
        <dependency>
            <groupId>org.jetbrains.kotlingroupId>
            <artifactId>kotlin-stdlib-jdk8artifactId>
            <version>${kotlin.version}version>
        dependency>
        <dependency>
            <groupId>org.jetbrains.kotlingroupId>
            <artifactId>kotlin-testartifactId>
            <version>${kotlin.version}version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.jetbrains.kotlingroupId>
            <artifactId>kotlin-maven-allopenartifactId>
            <version>${kotlin.version}version>
            <scope>providedscope>
        dependency>
    dependencies>

    <build>
        <sourceDirectory>src/main/kotlinsourceDirectory>
        <testSourceDirectory>src/test/kotlintestSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlingroupId>
                <artifactId>kotlin-maven-pluginartifactId>
                <version>${kotlin.version}version>

                <executions>
                    <execution>
                        <id>compileid>
                        <phase>compilephase>
                        <goals> <goal>compilegoal> goals>
                    execution>

                    <execution>
                        <id>test-compileid>
                        <phase>test-compilephase>
                        <goals> <goal>test-compilegoal> goals>
                    execution>
                executions>
                <configuration>
                    <jvmTarget>${java.version}jvmTarget>
                    <args>
                        <arg>-Xjsr305=strictarg>
                    args>
                    <compilerPlugins>
                        <plugin>all-openplugin>
                    compilerPlugins>
                    <pluginOptions>
                        <option>all-open:annotation=org.springframework.stereotype.Componentoption>
                    pluginOptions>
                configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlingroupId>
                        <artifactId>kotlin-maven-allopenartifactId>
                        <version>${kotlin.version}version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

代码

import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

fun main() = runBlocking<Unit>(Dispatchers.Default) {

        val mu1 = Mutex()
        val l1 = "t1"
        val mu2 = Mutex()
        val l2 = "t2"

        val p = Executors.newFixedThreadPool(4)

        repeat(3){
            p.execute{
                val tname = Thread.currentThread().name
                println("开始(外:$tname")
                val job = launch {
                    db(mu1,l1)
                }
                job.invokeOnCompletion {
                    println("结束(外:$tname")
                }
            }
        }
        repeat(3) {
            p.execute {
                val tname = Thread.currentThread().name
                println("开始(外:$tname")
                val job = launch {
                    db(mu1, l1)
                }
                job.invokeOnCompletion {
                    println("结束(外:$tname")
                }
            }
        }

        repeat(3) {
            p.execute {
                val tname = Thread.currentThread().name
                println("开始(外:$tname")
                val job = launch {
                    db(mu2, l2)
                }
                job.invokeOnCompletion {
                    println("结束(外:$tname")
                }
            }
        }
        repeat(3) {
            p.execute {
                val tname = Thread.currentThread().name
                println("开始(外:$tname")
                val job = launch {
                    db(mu2,l2)
                }
                job.invokeOnCompletion {
                    println("结束(外:$tname")
                }
            }
        }
        p.shutdown()
        //线程池等1秒结束,保活main线程,使得协程有机会执行完毕
        p.awaitTermination(1,TimeUnit.SECONDS)
    }
    
suspend fun db(mu:Mutex,s:String){
    val tname = Thread.currentThread().name
    mu.withLock {
        //这里的加锁相当于加了表级锁,可以用事务替换
        //模拟一次select count(*) > 0 ? update:insert
        println("select:$tname - $s")
        delay(50)
        println("insert/update:$tname - $s")
    }
    println("commit: $tname - $s")
}

你可能感兴趣的:(java,Maven,kotlin)