What is thread in java and how its methods behave

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

1. What is Thread

As in oracle java specification, thread is one of two basic units of execution, the other is process. Process was present prior to thread, we can refer to one process as an application. When an application is running, the resources allocated to the applicaition cannot be shared among other applications. The process of application exection is clear, but not efficent. Assume a application that has a lot of calculate tasks, execute task one by one, that will take a lot of hours. It can not be accpetable.

In order to make application execution more effcient, thread is introduced, also called light-weight process. One process has at least one thread. As mentioned above, application with a lot of calculate tasks divides all tasks into many smaller tasks, one small task is bounded to one thread, these task threads are able to run concurrently.

We can take advantage of thread, but there are disadvantages meanwhile. Threads share the resource of application, it may occurs race condition. The bahaviors of threads in race condition, particularly when not correctly synchronized, can be confusing and counterintuitive.

2. Creation of Thread

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass then should override the run method of class Thread. The Other way is to declare a class implements Runnable interface and implements the run method. An instance of that class then can be passed as as argument when creating an obejct of Thread.

3.Method in Thread

In this Section, We will focus on some important methods in Thread. These methods mainly fall into three types: Constructor method such as Thread(Runnable) and Thread(String), Static method such as yield() and sleep().and Instance method such as start() and join().

3.1 Constructor method

Constructor method is used to create a instance with keyword 'new', (except that declare a new class to be subclass of Thread supering any of Thread constructor methods in constructor method of subclass ). This part is easy-understanding, will not be discussed in this section.

3.2 Static method

Important static methods in Thread include sleep(long), sleep(long, int) and yield().

sleep(long) and sleep(long, int) cause the currently executing thread to sleep for specified number of milliseconds or plus nanoseconds, subject to the precision and accuracy of system timer and scheduler. This Thread does not lose the ownership of any monitors.

yield()  gives a hint to thread scheduler that the current thread is willing to yield its current use of processor. However, the hint is not mandatory, the scheduler is free to ignore this hint.

3.3 Static method

There are a lot of instance methods, for example: start(), join(long),  join(long, int), and interrupt()

Call start() to tell thread scheduler that this thread is ready to run , then the scheduler will schedule this thread to run in a proper time. It is never legal to start a thread more than once and a thread cannot restarted after it has completed execution.

join(long) and join(long, int)  suspend the current thread immediately until this thread dies or specified time is out,

interrupt() is to set to interrupt flag that is an indication to a thread to indicate that the thread should stop running. But it is up to programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. For more informations . you can refer to this website https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html.

 👉👉👉 自己搭建的租房网站:全网租房助手,m.kuairent.com,每天新增 500+房源

数字商品上架审计工单 /sku:付款人分类违禁扫描落地页上架分 【内容概要】 上架审计工单(命令面 /sku),把「有个想法」收成可复查判决:可上架 / 有条件上架 / 拒绝上架。检查谁付钱、违禁话术、买断数字价、落地页硬清单、上架分。支持 Cursor / Claude Code / Codex。金样 shelf-lab 校准「拒绝上架」(空包、无定价、违禁承诺)。 【适用人群】 适合:有半成品或清晰想法、卡在不敢定价、不敢点发布的人。 不适合:要代运营保销量、要保证收入数字、要复制他人支付链路的人。本包不保证销量。 【使用场景及目标】 安装 → 先跑金样看拒绝上架 → 再拿自己的想法开案。无明确买断价或关键落地页缺失,不得写成可上架。 【其他说明】 买断制,下载后不支持退款。不提供支付源码,不教违规获客话术。 立即下载

相关推荐

Team-Ai-Cli-Observability-Matrix-v1.0-原创源码与文档.zip

原创离线工具源码与完整文档,包含可运行示例、README、MIT LICENSE、原创与授权声明、自动化测试、真实运行截图及限制说明。下载解压后按 README 使用,无需外部密钥。仅供学习、研究与工程参考。

Java创建线程的两种方法

java创建线程的两种方法

web小菜鸟 575

校园招聘基于Python的智能匹配平台设计:多角色权限管理与岗位推荐系统实现 项目介绍 基于Python的校园招聘平台设计与实现(含模型描述及部分示例代码)

内容概要:本文详细介绍了一个基于Python的校园招聘平台的设计与实现,旨在通过信息化手段提升校园招聘的效率与精准度。平台采用Python主流框架(如Django/Flask)构建,涵盖用户权限管理、招聘与简历数据建模、智能匹配推荐、日志监控与统计分析等核心模块。系统支持学生、企业、就业部门等多角色协同,通过结构化数据模型和业务流程控制,实现了岗位发布、简历投递、状态流转、权限校验等功能,并结合TF-IDF与余弦相似度算法实现简历与岗位的智能匹配。代码示例展示了用户角色模型、企业岗位模型、简历分表设计、投递状态机、权限装饰器及推荐服务等关键实现,体现了系统的可扩展性与安全性设计。; 适合人群:具备Python Web开发基础,熟悉Django或Flask框架,有一定数据库设计和前后端交互经验的开发者,尤其是从事教育信息化、招聘系统开发或校园服务平台建设的研发人员;也适合计算机相关专业高年级本科生或研究生作为毕业设计参考。; 使用场景及目标:① 构建高校内部统一的校园招聘管理系统,替代传统低效的线下招聘模式;② 实现学生与企业岗位的智能匹配与个性化推荐,提升人岗匹配效率;③ 为企业和高校就业部门提供数据驱动的招聘分析与决策支持;④ 学习多角色权限控制、状态机设计、ORM建模、缓存与异步任务等实际开发技巧。; 阅读建议:此资源以实际项目为导向,不仅提供完整模型设计与代码片段,还深入剖析了系统架构与业务逻辑。建议读者结合代码示例搭建本地开发环境,动手实践模型定义、API接口开发与推荐算法集成,并重点关注权限控制、数据安全与性能优化等关键设计,以全面提升全栈开发与系统设计能力。

Java Thread Pools — Deep Dive

Java线程池核心原理摘要 线程池作用:解决线程创建开销大、资源耗尽问题,通过复用固定线程、控制并发量、任务队列解耦任务提交与执行。Java提供Executor框架(Java 5+)实现。 核心类层次: Executor → ExecutorService → ThreadPoolExecutor(核心)/ScheduledThreadPoolExecutor(定时) ForkJoinPool:工作窃取,支持并行流和CompletableFuture ThreadPoolExecutor关键参数: core

quwenqian的专栏 360

计算机为什么要用补码

目录 1. 补码诞生的背景 2. 原码、反码、补码 2.1 原码 2.2 反码 2.3 补码 3. 加减法 3.1普通算术加减法 3.2模N加减法 4. 总结 1. 补码诞生的背景 不论是在生活中还是虚拟网络中,人们总是习惯与10进制数字打交道,很容易理解10进制的加减乘除运算,但是我们知道计算机无法直接理解10进制,只能识别高低电平,一般人为设定0为低电平...

Turnhead的专栏 1万+

Lock vs Semaphore vs Condition Variable vs Monitor(中文)

在多线程(并发)程序中,多个线程会访问同一个共享内存,为了避免产生一些奇怪的结果,这些线程应该按照合适的顺序访问共享内存,这个过程称为同步(synchronization)。仅仅保持同步还不够,为了让同步更加高效,多个线程互相之间保持交流。 1.Critical Section(CS) 临界区(Critical section)是程序的一段代码块,临界区不能被多个线程在同一时间访问,临界区的访问是互斥的,某一时刻最多只有一个线程能进临界区。 2.Lock Lock提供了一种互斥的方式,Lo...

Turnhead的专栏 1430

重识JVM(2) - JAVA内存模型

目录 一、概念和误解 二、疑问 三、计算机的内存模型 3.1. 内存顺序和程序顺序 3.2. SC内存模型 3.3. TSO内存模型 3.4. 其他内存模型 3.5.JAVA内存模型 3.6. volatile实现原理 一、概念和误解 java内存模型,英文全称为java memory model,简称JMM。网上大部分关于java内存模型的资料都是关于堆栈...

Turnhead的专栏 537

Lock vs Semaphore vs Condition Variable vs Monitor

目录 1. Critical Section(CS) 2. Lock 3. Semaphore 4. Condition variable 5. Monitor 6. Summary In mutil-threads (concurrent) programing, two or more threads have access to a shared memory, for avoiding confusing results, these threads should acces...

Turnhead的专栏 465

ThreadLocal原理的秘密

目录 1. ThreadLocal描述 2. 认识ThreadLocalMap 3. 神奇的数字 4.注意 1. ThreadLocal描述 ThreadLocal保证了每个线程都有自己独享的变量,不用考虑并发同步的问题。通常情况下,ThreadLocal类型的变量被建议声明为static,即使多个线程调用多次,该变量也只会初始化一次。ThreadLocal使用起来很简单、方便,但非常值得弄清ThreadLocal的实现原理。 2. 认识ThreadLocalMap 在ThreadLo.

Turnhead的专栏 386

Producer-Consumer solution using wait(), notify(), park() and unpark()

1. Introduce In java, mutil-threads are used everywhere, applicationsnot only take advantages of mutil-threads improving efficiency, but also encounter some confusing problems especially when threads are not synchronized correctly. Fortunately, java prov.

Turnhead的专栏 286

The secret of ThreadLocal

ThreadLocal variable guareente each thread has own variable without thinking about synchronization. Usually threadlocal is d eclared static to be initialized only once whenever thread access it. We can easily use threadlocal in programm, but it is worth t

Turnhead的专栏 204

10kV变电所标准图.pdf.rar

10kV变电所标准图.pdf.rar

35kV屋内配电装置接线图.dwg.rar

35kV屋内配电装置接线图.dwg.rar

技术转移机构如何提升成果转化效率?.docx

技术转移机构如何提升成果转化效率?

IEC_60204-1-2021 CSV 机械电气设备 第 1 部分:通用技术条件 合并版

IEC_60204-1-2021 CSV 机械电气设备 第 1 部分:通用技术条件 中英两册

EF_6403.rar

EF_6403.rar

Casio说明书5174

Casio说明书5174

IMG_20260908_155227.jpg

IMG_20260908_155227.jpg

上一篇: 重识JVM(2) - JAVA内存模型
下一篇: A thread shoud not be controlled directly by other threads
祥先生
博客等级 码龄14年 48粉丝 25原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值