Thread(上) 【015】

本文介绍了Java中创建和管理线程的多种方法,包括继承Thread类和实现Runnable接口两种常见方式,还探讨了线程的中断、同步、优先级设置、停止及yield方法的使用。
//、、、、、[color=red]创建线程的方法一[/color]、、、、、、、、、、、、、、、、、、、\\

[code]
package com.testthread;

public class TestThread2 {
public static void main(String args[]) {
Runner2 r = new Runner2() ;
r.start() ;
for(int i=0; i<100; i++) {
System.out.println("Main thread:-----" + i) ;
}
}
}
class Runner2 extends Thread { //通过继承Thread 类创建线程
public void run() {
for(int i=0; i<30; i++) {
System.out.println("Runner2:--" + i) ;
}
}
}
[/code]

//、、、、、[color=red]创建线程的方法二[/color]、、、、、、、、、、、、、、、、、、、\\
[code]
package com.testthread; //默认导入java.lang包
public class TestThread1 {
public static void main(String args[]) {
Runner1 r = new Runner1() ;
Thread th = new Thread(r) ;
th.start(); //线程启动
r.run(); //这个是方法调用,不能使新线程启动

for(int i=0; i<100; i++) {
System.out.println("Main thread:-----" +i) ;
}
}
}
class Runner1 implements Runnable { // 通过实现runnable接口创建和启动线程,尽量使用该方法而少用(不用)继承Thread方法
public void run() {
for(int i=0; i<30; i++) {
System.out.println("Runner1------"+i) ;
}
}
}
[/code]
//。。。。。[color=red]TestInterrupted[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;
import java.util.* ;
public class TestInterrupted {
public static void main(String args[]) {
Thread th = new Thread(new MyThread()) ;
th.start();
try {
Thread.sleep(10000) ; //该sleep 是让Main 线程休眠10秒,让th 线程工作
} catch (InterruptedException e) {
e.printStackTrace();
}
th.interrupt(); //中断线程,MyThread 里的catch
}
}
class MyThread implements Runnable {
public void run() {
while(true) {
System.out.println("===" + new Date() +"===") ; //输出系统时间,输出十次,因为main休眠10秒
try {
Thread.sleep(1000); // sleep 是static 属性,可以直接调用。让该线程休眠1秒
} catch (InterruptedException e) {
return; // 当休眠被打断时停止
}
}
}
}
[/code]

//。。。。。[color=red]TestJoin[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;

public class TestJoin {
public static void main(String[] args) {
MyThread1 m = new MyThread1("New") ;
m.start();
try {
m.join(); //等待m线程终止才继续往下进行,和调用run方法结果一样
} catch (InterruptedException e) {
e.printStackTrace();
}

for(int i=1; i<=10; i++) {
System.out.println("This is main thread.") ;
}
}
}
class MyThread1 extends Thread {
MyThread1(String s) {
super(s) ; //调用Thread中的构造方法
}

public void run() {
for(int i=1; i<=10; i++) {
System.out.println("This is" + getName()); //返回该线程的名称
try {
sleep(1000) ;
} catch (InterruptedException e) {
return ;
}
}
}
}
[/code]


//。。。。。[color=red]TestPriority[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;

public class TestPriority {
public static void main(String args[]) {
Thread t1 = new Thread(new MyThread3()) ;
Thread t2 = new Thread(new MyThread4()) ;
t1.setPriority(Thread.NORM_PRIORITY + 3) ; //Thread.NORM_PRIORITY---分配给线程的默认优先级
t1.start() ;
t2.start() ;
}
}

class MyThread3 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("MyThread3: " + i) ;
}
}
}

class MyThread4 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("-------Mythreads4: " + i) ;
}
}
}
[/code]

//。。。。。[color=red]TestStop[/color]。。。。。。。。。。。。。。。。。。\\

[code]
package com.testthread;

public class TestStop {
public static void main(String args[]) {
ThreadRun tr = new ThreadRun() ;
Thread r = new Thread(tr);
r.start();
for (int i = 0; i < 1000000; i++) {
if (i % 1000 == 0) {
System.out.println("in thread main i=" + i);
}
}
System.out.println("Thread main is over");
tr.shutDown() ;
r.stop();
}
}

class ThreadRun implements Runnable {
private boolean flag = true;

public void run() {
int i = 0 ;
while (flag = true) {
System.out.print(" " + i++);
}
}

public void shutDown() {
flag = false ;
}
}

[/code]

//。。。。。[color=red]TestYield[/color]。。。。。。。。。。。。。。。。。。\\

[code]
package com.testthread;
public class TestYield {
public static void main(String args[]) {
MyThread2 th1 = new MyThread2("th1") ;
MyThread2 th2 = new MyThread2("th2") ;
th1.start();
th2.start();
}
}

class MyThread2 extends Thread {
public MyThread2(String s) {
super(s) ;
}

public void run() {
for(int i=1; i<=100; i++) {
System.out.println(getName()+ ": "+i) ;
if(i%10 == 0) {
yield() ; //当i值是10的倍数时候,暂停当前正在执行的线程对象,并执行其他线程
}
}
}
}

[/code]
2025-10-23 16:08:18,015 INFO o.a.j.t.JMeterThread: Thread is done: 线程组 1-1 2025-10-23 16:08:18,015 INFO o.a.j.t.JMeterThread: Thread finished: 线程组 1-1 2025-10-23 16:08:18,015 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2025-10-23 16:08:18,015 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*) 2025-10-23 16:08:18,261 INFO o.a.j.e.StandardJMeterEngine: Running the test! 2025-10-23 16:08:18,261 INFO o.a.j.s.SampleEvent: List of sample_variables: [] 2025-10-23 16:08:18,261 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*) 2025-10-23 16:08:18,287 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : 线程组 2025-10-23 16:08:18,287 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group 线程组. 2025-10-23 16:08:18,287 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error 2025-10-23 16:08:18,287 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false 2025-10-23 16:08:18,287 INFO o.a.j.t.ThreadGroup: Started thread group number 1 2025-10-23 16:08:18,287 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started 2025-10-23 16:08:18,289 INFO o.a.j.t.JMeterThread: Thread started: 线程组 1-1 2025-10-23 16:08:18,305 INFO o.a.j.t.JMeterThread: Thread is done: 线程组 1-1 2025-10-23 16:08:18,305 INFO o.a.j.t.JMeterThread: Thread finished: 线程组 1-1 2025-10-23 16:08:18,305 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2025-10-23 16:08:18,305 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
10-24
2025-09-21 09:34:06.807 WARNING streamlit.runtime.scriptrunner_utils.script_run_context: Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.015 Warning: to view this Streamlit app on a browser, run it with the following command: streamlit run Z:\PythonProject_9_19_GBDT\CZD_9_19_RFR.py [ARGUMENTS] 2025-09-21 09:34:07.015 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.015 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.015 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.015 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.016 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.016 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.016 Session state does not function when running a script without `streamlit run` 2025-09-21 09:34:07.016 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.016 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. 2025-09-21 09:34:07.016 Thread 'MainThread': missing ScriptRunContext! This warning can be ignored when running in bare mode. Traceback (most recent call last): File "Z:\PythonProject_9_19_GBDT\CZD_9_19_RFR.py", line 272, in <module> main() File "Z:\PythonProject_9_19_GBDT\CZD_9_19_RFR.py", line 93, in main if coag_models[0] is None: ~~~~~~~~~~~^^^ TypeError: 'NoneType' object is not subscriptable
09-22
本设计项目聚焦于一款面向城市环保领域的移动应用开发,该应用以微信小程序为载体,结合SpringBoot后端框架与MySQL数据库系统构建。项目成果涵盖完整源代码、数据库结构文档、开题报告、毕业论文及功能演示视频。在信息化进程加速的背景下,传统数据管理模式逐步向数字化、系统化方向演进。本应用旨在通过技术手段提升垃圾分类管理工作的效率,实现对海量环保数据的快速处理与整合,从而优化管理流程,增强事务执行效能。 技术上,前端界面采用VUE框架配合layui样式库进行构建,小程序端基于uni-app框架实现跨平台兼容;后端服务选用Java语言下的SpringBoot框架搭建,数据存储则依托关系型数据库MySQL。系统为管理员提供了包括用户管理、内容分类(如环保视频、知识、新闻、垃圾信息等)、论坛维护、试题与测试管理、轮播图配置等在内的综合管理功能。普通用户可通过微信小程序完成注册登录,浏览各类环保资讯、查询垃圾归类信息,并参与在线知识问答活动。 在设计与实现层面,该应用注重界面简洁性与操作逻辑的一致性,在满足基础功能需求的同时,也考虑了数据安全性与系统稳定性的解决方案。通过模块化设计与规范化数据处理,系统不仅提升了管理工作的整体效率,也推动了信息管理的结构化与自动化水平。整体而言,本项目体现了现代软件开发技术在环保领域的实际应用,为垃圾分类的推广与管理提供了可行的技术支撑。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值