首页 > Java > java教程 > 正文

多线程 Cap 编程

霞舞
发布: 2024-11-20 08:51:01
转载
1169人浏览过

主要技能和概念
• 了解创建多线程的基础知识
• 了解 thread 类和 runnable
接口 • 创建一个线程
• 创建多个线程
• 确定线程何时结束
• 使用线程优先级
• 了解线程同步
• 使用同步方法
• 使用同步块
• 促进线程之间的通信
• 暂停、恢复和停止线程

线程:这些是程序内独立的执行路径。
多任务:它可以基于进程(多个程序)或线程(同一程序中的多个任务)。
优点:
利用空闲时间提高效率。
更好地利用多核/多处理器系统。

线程创建和管理

类和接口:
thread:封装线程的类。
runnable:用于定义自定义线程的接口。

常用线程类方法:

  • getname():返回线程的名称。
  • getpriority():返回优先级。
  • isalive():检查线程是否仍在执行。
  • join():等待线程完成。
  • run():定义线程的入口点。
  • sleep(long ms):暂停线程一段时间。
  • start(): 开始线程执行。

创建主题:

  • 实现可运行:
class mythread implements runnable {
    string threadname;

    mythread(string name) {
        threadname = name;
    }

    public void run() {
        system.out.println(threadname + " starting.");
        try {
            for (int i = 0; i < 10; i++) {
                thread.sleep(400);
                system.out.println("in " + threadname + ", count is " + i);
            }
        } catch (interruptedexception e) {
            system.out.println(threadname + " interrupted.");
        }
        system.out.println(threadname + " terminating.");
    }
}

public class usethreads {
    public static void main(string[] args) {
        system.out.println("main thread starting.");

        mythread mythread = new mythread("child #1");
        thread thread = new thread(mythread);
        thread.start();

        for (int i = 0; i < 50; i++) {
            system.out.print(".");
            try {
                thread.sleep(100);
            } catch (interruptedexception e) {
                system.out.println("main thread interrupted.");
            }
        }
        system.out.println("main thread ending.");
    }
}

登录后复制

预期输出:

main thread starting.
.
child #1 starting.
..
in child #1, count is 0
...
in child #1, count is 1
...
main thread ending.

登录后复制

线程类扩展:

class MyThread extends Thread {
    MyThread(String name) {
        super(name);
    }

    public void run() {
        System.out.println(getName() + " starting.");
        try {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(400);
                System.out.println("In " + getName() + ", count is " + i);
            }
        } catch (InterruptedException e) {
            System.out.println(getName() + " interrupted.");
        }
        System.out.println(getName() + " terminating.");
    }
}

public class UseThreads {
    public static void main(String[] args) {
        System.out.println("Main thread starting.");

        MyThread thread = new MyThread("Child #1");
        thread.start();

        for (int i = 0; i < 50; i++) {
            System.out.print(".");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("Main thread interrupted.");
            }
        }
        System.out.println("Main thread ending.");
    }
}

登录后复制

注意:sleep()方法使得调用它的线程暂停执行
在指定的毫秒时间内。

书桌
多线程 Cap 编程

以上就是多线程 Cap 编程的详细内容,更多请关注php中文网其它相关文章!

豆包AI编程
豆包AI编程

智能代码生成与优化,高效提升开发速度与质量!

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号