线程的让步

线程让出自己占用的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()

最新文章

  1. Windows Azure Cloud Service (40) 使用VS2013的publishSettings文件,发布Cloud Service
  2. WebView的返回功能
  3. git Unstaged changes after reset
  4. SQl函数的写法
  5. bzoj1001
  6. [设计模式]观察者模式1(用JDK提供的API)
  7. Opencart 之 Registry 类详解
  8. ecshop json类的使用
  9. Makefile与Shell的问题
  10. 编译器DIY——词法分析
  11. javascript代码实现简单的五星评价功能!
  12. System.Object简介
  13. c/c++ 模板 类型推断
  14. 学习使用Mendeley1
  15. Python基础语法总结
  16. asp.net core配置文件
  17. 远程显示(操作) 服务器 GUI 程序(图形化界面) (基于 X11 Forwarding + Centos + MobaXterm)
  18. 系统空间占用排查 tomcat超大日志catalina.out 删除 与df 状态更新
  19. keepalived+mysql 高可用集群
  20. Java中static代码块,{}大括号代码块,构造方法代码块执行顺序!

热门文章

  1. 为什么使用Stylus
  2. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Highway
  3. 今天发现一个汉字转换成拼音的模块,记录一下,直接pip install xpinyin即可
  4. Chrome下font-size小于12px的解决办法
  5. python_11(网络编程)
  6. P2006 赵神牛的游戏
  7. AJPFX关于Java内部类及其实例化
  8. redis安装(windows)
  9. HttpServletResponse 的状态码
  10. transform、transition 和 animation区别