线程的让步

线程让出自己占用的CPU资源

  • 线程让出资源,不指定让给谁
  • 线程让出资源,指定让给谁

方法1:

public static void yield();

线程实现交替打印

import java.io.*;
import java.util.*;
class MyRunnable implements Runnable{
private String l;
private String r;
public MyRunnable(String fl,String fr) {
this.l=fl;this.r=fr;
}
public void run() {
for(int i=0;i<30;i++) {
System.out.print(l+" "+i+" "+r+" ");
Thread.yield();
}
}
}
public class Main {
public static void main(String[] args) {
MyRunnable mr1=new MyRunnable("[","]");
MyRunnable mr2=new MyRunnable("(",")");
Thread t1=new Thread(mr1);
Thread t2=new Thread(mr2);
t1.start();t2.start();
}
}

方法2:

public final void join() throws InterruptedException
public final void join(long millis) throws InterruptedException
public final void join(long millis,int nano) throws InterruptedException

将两个线程合并为同一个线程,A调用join B,A等到B结束再恢复执行

import java.io.*;
import java.util.*;
class MyRunnable implements Runnable{
private String l;
private String r;
public MyRunnable(String fl,String fr) {
this.l=fl;this.r=fr;
}
public void run() {
for(int i=0;i<30;i++) {
System.out.print(l+" "+i+" "+r+" ");
Thread.yield();
}
}
}
public class Main {
public static void main(String[] args) {
MyRunnable mr1=new MyRunnable("[","]");
Thread t1=new Thread(mr1);
t1.start();
for(int i=0;i<30;i++) {
if(i==10) {
try {
System.out.print("Join");
t1.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("( "+i+" )");
}
}
}

方法3:

public final void setDaemon(boolean on) //将某个线程设置为守护线程

方法4:

public static void sleep(long millis)throws InterruptedExcepiton

线程同步


方法1:

synchronized 加锁
class Resources{
synchronized void function1(Thread currThread) {
for(int i=0;i<300;i++) {
System.out.println(currThread.getName());
}
}
}
class MyThread extends Thread{
Resources rs;
public MyThread(String tname,Resources trs) {
this.setName(tname);
this.rs=trs;
}
public void run() {
if(this.getName().equals("Thread1")) {
System.out.println("Thread1启动,等待进入function1");
rs.function1(this);
}else {
System.out.println("Thread2启动,等待进入function1");
rs.function1(this);
}
}
}
public class Main {
public static void main(String[] args) {
Resources rs=new Resources();
MyThread t1=new MyThread("Thread1",rs);
MyThread t2=new MyThread("Thread2",rs);
t1.start();t2.start();
}

方法2:

public final void wait() throws InterruptedException
public final void wait(long timeout) throws InterruptedException
public final void notify()
public final void notifyAll()

Java多线程入门Ⅱ的更多相关文章

  1. java多线程入门学习(一)

    java多线程入门学习(一) 一.java多线程之前 进程:每一个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销.一个进程包括1--n个线程.     线程:同一类线程共享代码 ...

  2. Java多线程学习(一)Java多线程入门

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79640870 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  3. (转载)Java多线程入门理解

    转载出处http://blog.csdn.net/evankaka 写在前面的话:此文只能说是java多线程的一个入门,其实Java里头线程完全可以写一本书了,但是如果最基本的你都学掌握好,又怎么能更 ...

  4. Java 多线程入门

    进程与线程 在学习Java多线程之前,先简单复习一下进程与线程的知识. 进程:进程是系统进行资源分配和调度的基本单位,可以将进程理解为一个正在执行的程序,比如一款游戏. 线程:线程是程序执行的最小单位 ...

  5. java多线程入门

    一.认识多任务.多进程.单线程.多线程 要认识多线程就要从操作系统的原理说起.   以前古老的DOS操作系统(V 6.22)是单任务的,还没有线程的概念,系统在每次只能做一件事情.比如你在copy东西 ...

  6. Java多线程入门知识点梳理

    前言 在多核时代,高并发时代,对系统并行处理能力有很高要求.多线程就是这个时代最好的产物.通过使用多线程可以增强系统并行处理能力,提高CPU资源的有效利用:从而提高系统的处理能力.常见应用场景如:多窗 ...

  7. Java多线程入门及实战

    基本概念: 1: 程序 2 进程 3 线程 4 进程和线程的区别 5 进程和程序的区别 Java实现多线程的方法: 1 继承Thread 2 实现Runable 3 实现callable 4 线程池的 ...

  8. Java多线程入门中几个常用的方法

    一.currentThread()方法 currentThread方法就是返回当前被调用的线程. 该方法为一个本地方法,原码如下: /** * Returns a reference to the c ...

  9. Java多线程学习(四)等待/通知(wait/notify)机制

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79690279 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

随机推荐

  1. 关于使用IQKeyBoardManager键盘还是被遮挡的问题解决方案

    今天在做一个登录界面的时候发现使用了IQKeyBoardManager键盘还是被遮挡,解决方案如下 解决方案一:在所有视图的最外层添加一个UIView作为容器即可,但在有导航栏的情况下导航栏会跟着向上 ...

  2. 跟我一起玩Win32开发(4):创建菜单

    也不知道发生什么事情,CSDN把我的文章弄到首页,结果有不少说我在误人子弟,是啊,我去年就说过了,如果你要成为砖家级人物,请远离我的博客,我这个人没什么特长,唯一厉害的一点就是不相信权威,鄙视砖家,所 ...

  3. redis优势

    redis是高性能的key-value内存数据库. 由于是内存型的,所以性能相比磁盘数据库更加优秀. 由于支持丰富的数据类型,相比memcache更受开发者欢迎.列表和整形是最常用的数据类型. 就算对 ...

  4. 题解报告:hdu 1392 Surround the Trees(凸包入门)

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  5. 题解报告:hdu1231最大连续子序列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 Problem Description 给定K个整数的序列{ N1, N2, ..., NK } ...

  6. JAVA常用知识总结(四)——集合

    先附一张java集合框架图 下面根据面试中常问的关于集合的问题进行了梳理: Arraylist 与 LinkedList 有什么不同? 1. 是否保证线程安全: ArrayList 和 LinkedL ...

  7. PDO相关函数

    (PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0) PDO::__construct — 创建一个表示数据库连接的 PDO 实例 说明 PDO::__co ...

  8. (办公)定时任务quartz入门

    1.简单入门. 2.定时任务注入service. 入门案例: 1.1. 加jar <dependency> <groupId>org.quartz-scheduler</ ...

  9. H+后台主题UI框架---整理(一)

    本篇文章是对H+这种框架进行整理,顺便了解一下标准的代码规范的写法. 一.表单: 1).下面是一个基本表单: 现在来看这个表单的结构: 1.整个表单的外框结构是一个div,至于padding和marg ...

  10. 一段字符串中间提取json字符串

    项目过程中经常打日志:LOG.error("[failure][CreateOrder] param:{}", JSON.toJSONString(userCreateOrderD ...