Java多线程方法有哪些
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,这篇文章主要介绍"Java多线程方法有哪些",在日常操作中,相信很多人在Java多线程方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Java多线程方法有哪些"
千家信息网最后更新 2025年11月14日Java多线程方法有哪些
这篇文章主要介绍"Java多线程方法有哪些",在日常操作中,相信很多人在Java多线程方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Java多线程方法有哪些"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一、interrupt方法一种让线程退出的方式。
import java.util.*; public class TestInterrupt{ public static void main(String[] args){ MyThread t = new MyThread(); t.start(); try{Thread.sleep(10000);} catch(InterruptedException i){} t.interrupt(); } } class MyThread extends Thread{ public void run(){ while(true){ try{ System.out.println("------"+new Date()+"-----"); Thread.sleep(1000); }catch(InterruptedException i){ return; } } } }二、join和yield方法
t.join(); //t的run()方法完才会继续执行当前线程方法体
//也就是两个线程变成了一个线程
t.yield(); //暂停当前正在执行的线程对象,并执行其他线程。方法为静态
//哪个线程体执行此方法,哪个线程让步
public class TestYield { public static void main(String[] args) { MyThread3 t1 = new MyThread3("t1"); MyThread3 t2 = new MyThread3("t2"); t1.start(); t2.start(); } } class MyThread3 extends Thread { MyThread3(String s){super(s);} public void run(){ for(int i =1;i<=100;i++){ System.out.println(getName()+": "+i); if(i==0){ yield(); } } } }三、线程优先级别
线程的优先级用数字表示,范围从1到10,一个线程的缺省优先级为5.
Thread.MAX_PRIORITY=1
Thread.MIN_PRIORITY=10
Thread.NORM_PRIORITY=5
例:t.setPriority(Thread.NORM_PRIORITY+3);
四、线程同步
1.同步代码块
synchronized(this){ //在执行代码块过程中,不会被其他线程打断
...
}
public sunchronized void method //执行此方法时,当前对象被锁定
在Java语言中,引入了对象互斥锁的概念,保证共享数据操作的完整性,每个对象 都对应一个可称为"互斥锁"的标记,这个标记保证在任一时刻,只能有一个线程访 问该对象。
2.线程死锁
public class TestDeadLock implements Runnable { public int flag = 1; static Object o1 = new Object(), o2 = new Object(); public void run() { System.out.println("flag=" + flag); if(flag == 1) { synchronized(o1) { try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } synchronized(o2) { System.out.println("1"); } } } if(flag == 0) { synchronized(o2) { try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } synchronized(o1) { System.out.println("0"); } } } } public static void main(String[] args) { TestDeadLock td1 = new TestDeadLock(); TestDeadLock td2 = new TestDeadLock(); td1.flag = 1; td2.flag = 0; Thread t1 = new Thread(td1); Thread t2 = new Thread(td2); t1.start(); t2.start(); } }五、生产者消费者问题
public class ProducerConsumer { public static void main(String[] args) { SyncStack ss = new SyncStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); new Thread(p).start(); new Thread(p).start(); new Thread(p).start(); new Thread(c).start(); } } class WoTou { int id; WoTou(int id) { this.id = id; } public String toString() { return "WoTou : " + id; } } class SyncStack { //栈实现 int index = 0; WoTou[] arrWT = new WoTou[6]; //相当于装物品的篮子 public synchronized void push(WoTou wt) { //生产物品,线程安全 while(index == arrWT.length) { //当篮子满了线程等待 try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); //开始生产时,叫醒等待的其他线程开始消费 arrWT[index] = wt; index ++; } public synchronized WoTou pop() { //消费物品,线程安全 while(index == 0) { //如果篮子空了 try { this.wait(); //线程等待,等待生产者开始 //生产,叫醒此线程 } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); //消费时喊醒生产者生产 index--; return arrWT[index]; } } class Producer implements Runnable { //生产者类 SyncStack ss = null; Producer(SyncStack ss) { this.ss = ss; } public void run() { for(int i=0; i<20; i++) { //生产20个 WoTou wt = new WoTou(i); ss.push(wt); System.out.println("生产了:" + wt); try { Thread.sleep((int)(Math.random() * 200)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { SyncStack ss = null; Consumer(SyncStack ss) { this.ss = ss; } public void run() { for(int i=0; i<20; i++) { //消费20个 WoTou wt = ss.pop(); System.out.println("消费了: " + wt); try { Thread.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } }到此,关于"Java多线程方法有哪些"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
线程
方法
生产
消费
对象
生产者
学习
物品
篮子
安全
代码
优先级
更多
标记
此方法
问题
保证
同步
帮助
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发怎样编程
计算机显示dns服务器错误
中流房地一体数据库
明日之后连接服务器怎么回事
华为软件开发年龄限制
2021网络安全共享网络文明
无界面服务器与qt客户端之间
重庆维普数据库中刊
直销软件开发软盟
河北标准软件开发密度
反向代理服务器DNS
腾讯网络安全数字收藏品
互联网 科技项目
wpf分页的时候数据库改变了
手机pubg服务器延迟
网络安全管理述职报告范文
3dmax服务器激活
方舟私人服务器可以上传吗
请问北大青鸟读软件开发
贵州农业职校计算机网络技术
数据库与网络工程师哪个好
修改校园网服务器访问人数很难吗
招盟网络技术有限公司
服务器有几个cpu
iatf嵌入式软件开发
穿越火线商城服务器
网络安全保护法规定网络运营者
郑州erp软件开发招聘
软件开发质量月报
永隆银行软件开发中心