在多线程中所有的操作方法都是从Thread类开始的,所有的操作基本都在Thread类中。

第一取得线程名字

a,在Thread类中,可以通过getName()方法,获得线程的名字,可以通过setName()方法设置线程的名字

b,线程名字一般在线程启动前设置,但是也允许为已经运行的线程设置名称,允许2个Thread对象有相
同的名字,但是不推荐,你懂的!!!

c,如果程序没有为线程指定名字,则系统自动为线程分配一个名称。

    package xianchengcaozuo;  

    public class ThreadName {
public static void main(String[] args) {
Demo d1= new Demo();
Thread t1= new Thread(d1);
Thread t2= new Thread(d1,"线程A");
Thread t3= new Thread(d1,"线程B");
Thread t4= new Thread(d1);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Demo implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for(int i=;i<;i++){
System.out.println(Thread.currentThread().getName()+"运行,i="+i);
} } }
Thread-0运行,i=0
Thread-0运行,i=1
Thread-0运行,i=2
线程A运行,i=0
线程A运行,i=1
线程A运行,i=2
Thread-0运行,i=3
Thread-0运行,i=4
线程A运行,i=3
线程A运行,i=4
线程B运行,i=0
线程B运行,i=1
线程B运行,i=2
线程B运行,i=3
线程B运行,i=4
Thread-1运行,i=0
Thread-1运行,i=1
Thread-1运行,i=2
Thread-1运行,i=3
Thread-1运行,i=4

没指定名字,则按编号!!

第二 获取当前线程对象currentThread()

第三判断线程是否启动成功 isAlive()

第四 强制运行线程

    package xianchengcaozuo;
/*
* 强制运行线程用join方法,期间其它线程无法运行,必须等待此线程
* 执行完后,其它线程才可以执行
*/
public class Qiangzhi {
public static void main(String[] args) {
Demo1 d1= new Demo1();
Thread t1= new Thread(d1,"线程A");
t1.start();
for(int i=0;i<50;i++){
if(i>10){
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("main线程运行:"+i);
} }
}
class Demo1 implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()+"运行,i="+i);
} } }

结果:

main线程运行:0
main线程运行:1
main线程运行:2
main线程运行:3
main线程运行:4
main线程运行:5
main线程运行:6
main线程运行:7
main线程运行:8
main线程运行:9
main线程运行:10
线程A运行,i=0
线程A运行,i=1
线程A运行,i=2
线程A运行,i=3
线程A运行,i=4
线程A运行,i=5
线程A运行,i=6
线程A运行,i=7
线程A运行,i=8
线程A运行,i=9
线程A运行,i=10
线程A运行,i=11
线程A运行,i=12
线程A运行,i=13
线程A运行,i=14
线程A运行,i=15
线程A运行,i=16
线程A运行,i=17
线程A运行,i=18
线程A运行,i=19
线程A运行,i=20
线程A运行,i=21
线程A运行,i=22
线程A运行,i=23
线程A运行,i=24
线程A运行,i=25
线程A运行,i=26
线程A运行,i=27
线程A运行,i=28
线程A运行,i=29
线程A运行,i=30
线程A运行,i=31
线程A运行,i=32
线程A运行,i=33
线程A运行,i=34
线程A运行,i=35
线程A运行,i=36
线程A运行,i=37
线程A运行,i=38
线程A运行,i=39
线程A运行,i=40
线程A运行,i=41
线程A运行,i=42
线程A运行,i=43
线程A运行,i=44
线程A运行,i=45
线程A运行,i=46
线程A运行,i=47
线程A运行,i=48
线程A运行,i=49
main线程运行:11
main线程运行:12
main线程运行:13
main线程运行:14
main线程运行:15
main线程运行:16
main线程运行:17
main线程运行:18
main线程运行:19
main线程运行:20
main线程运行:21
main线程运行:22
main线程运行:23
main线程运行:24
main线程运行:25
main线程运行:26
main线程运行:27
main线程运行:28
main线程运行:29
main线程运行:30
main线程运行:31
main线程运行:32
main线程运行:33
main线程运行:34
main线程运行:35
main线程运行:36
main线程运行:37
main线程运行:38
main线程运行:39
main线程运行:40
main线程运行:41
main线程运行:42
main线程运行:43
main线程运行:44
main线程运行:45
main线程运行:46
main线程运行:47
main线程运行:48
main线程运行:49

第五 线程的休眠

    package xianchengcaozuo;  

    import java.util.Date;  

    /*
* 通过sleep方法,可以使线程暂停执行
*
*/
public class ThreadSleep {
public static void main(String[] args) {
Demo2 d1= new Demo2();
Thread t1= new Thread(d1,"线程C");
t1.start();
}
} class Demo2 implements Runnable{ @Override
public void run() { for(int i=0;i<10;i++){
try {
//此处是毫秒
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(new Date());
System.out.println(Thread.currentThread().getName()+"运行,i="+i);
}
} }
    Tue Apr 22 11:10:40 CST 2014
线程C运行,i=0
Tue Apr 22 11:10:45 CST 2014
线程C运行,i=1
Tue Apr 22 11:10:50 CST 2014
线程C运行,i=2
Tue Apr 22 11:10:55 CST 2014
线程C运行,i=3
Tue Apr 22 11:11:00 CST 2014
线程C运行,i=4

线程终止:

如果主线程是5000,那么会先run正常,在被终止

    package xianchengcaozuo;
//当一个线程运行的时候,另一个线程可以通过interrupt方法种植其线程. class MyThread09 implements Runnable {
public void run() {
System.out.println("1、进入run方法");
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(5000);
System.out.println("2、休眠已经完成");
} catch (Exception e) {
System.out.println("3、休眠被终止");
return;
}
System.out.println("4、run方法正常结束");
}
}
}
class Threadinterrupt{
public static void main(String[] args) {
MyThread09 my = new MyThread09();
Thread t = new Thread(my,"线程");
t.start();
try {
Thread.sleep(2000);
} catch (Exception e) {
}
t.interrupt();
}
}
    1、进入run方法
3、休眠被终止

线程的优先级:

    package xianchengcaozuo;  

    public class Threadjibie {
public static void main(String[] args) {
Demo5 my = new Demo5();
Thread t1 = new Thread(my,"线程A");
Thread t2 = new Thread(my,"线程B");
Thread t3 = new Thread(my,"线程C");
//设置线程优先级
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t3.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
class Demo5 implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + "运行,i = " + i);
}
}
}
    线程C运行,i = 0
线程B运行,i = 0
线程A运行,i = 0
线程C运行,i = 1
线程B运行,i = 1
线程A运行,i = 1
线程C运行,i = 2
线程B运行,i = 2
线程A运行,i = 2
线程C运行,i = 3
线程B运行,i = 3
线程A运行,i = 3
线程C运行,i = 4
线程B运行,i = 4
线程A运行,i = 4

主方法的优先级是中级

线程的礼让:

    package xianchengcaozuo;
//yield(),线程礼让,将操作让给其它线程
public class ThreadLirang {
public static void main(String[] args) {
Demo6 d1= new Demo6(); Thread t1 = new Thread(d1);
Thread t2 = new Thread(d1);
t1.start();
t2.start(); }
}
class Demo6 implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+"运行,i="+i);
if(i==3){
Thread.currentThread().yield();
System.out.println(Thread.currentThread().getName()+"礼让");
}
}
} }

结果: Thread-1运行,i=0
Thread-1运行,i=1
Thread-1运行,i=2
Thread-1运行,i=3
Thread-0运行,i=0
Thread-0运行,i=1
Thread-0运行,i=2
Thread-0运行,i=3
Thread-0礼让
Thread-0运行,i=4
Thread-1礼让
Thread-1运行,i=4

多线程操作的方法(sleep,)setPriority(Thread.MIN_PRIORITY);yield();的更多相关文章

  1. Thread中yield方法

    先上一段代码 public class YieldExcemple { public static void main(String[] args) { Thread threada = new Th ...

  2. (转)Thread中yield方法

    先上一段代码 public class YieldExcemple { public static void main(String[] args) { Thread threada = new Th ...

  3. Android多线程操作,as快捷键笔记

    Android studio 快捷键 cmd+p 快速查看该方法的参数定义 * * option + shift +上下 快速移动上下行 * * cmd + e 显示最近操作的文件 * * cmd + ...

  4. Java自学-多线程 常见线程方法

    Java 常见的线程方法 示例 1 : 当前线程暂停 Thread.sleep(1000); 表示当前线程暂停1000毫秒 ,其他线程不受影响 Thread.sleep(1000); 会抛出Inter ...

  5. C# winform编程中多线程操作控件方法

    private void Form1_Load(object sender, EventArgs e) { Thread newthread = new Thread(new ThreadStart( ...

  6. sqlite:多线程操作数据库“database is locked”解决方法(二)

    上一篇博客<sqlite:多线程操作数据库“database is locked”解决方法>通过注册延时函数的方法来处理数据库被锁的问题.此方法固然能解决问题,但是在多个线程向数据库写入大 ...

  7. java多线程基本概述(二)——Thread的一些方法

    在Thread类中有很多方法值得我们关注一下.下面选取几个进行范例: 1.1.isAlive()方法 java api 描述如下: public final boolean isAlive() Tes ...

  8. 创建多线程的第一种方式——创建Thread子类和重写run方法

    创建多线程的第一种方式——创建Thread子类和重写run方法: 第二种方式——实现Runnable接口,实现类传参给父类Thread类构造方法创建线程: 第一种方式创建Thread子类和重写run方 ...

  9. 2016/1/25 多线程 作业 方法一 继承Thread 方法二 实现Runnable 多线程笔记

    /* * 1,尝试定义一个继承Thread类的类,并覆盖run()方法, * 在run()方法中每隔100毫秒打印一句话.*/ package Stream; //方法一 继承Thread 实现多线程 ...

随机推荐

  1. Redis Eval Script

    简介 从Redis 2.6 版本开始,内嵌支持 Lua 环境.通过使用EVAL或EVALSHA命令可以使用 Lua 解释器来执行脚本. EVAL和EVALSHA的使用是差不多的(下面有讲区别). EV ...

  2. MVC架构介绍-框架分层

    实例产品基于asp.net mvc 5.0框架,源码下载地址:http://www.jinhusns.com/Products/Download Tunynet.Infrastructurs 是我们自 ...

  3. 【开发工具之eclipse】7、eclipse代码自动提示,eclipse设置代码自动提示

    首先打开Eclipse开发软件,然后在工具栏的[Window],点击后找到弹出列表中的[Preferences]选项,然后点击这个选项   现在弹出的窗口就是JAVA开发人员常见的设置窗口了,我们依次 ...

  4. 【Java面试】1、基础知识篇

    [Java面试]基础知识篇 Java基础知识总结,主要包括数据类型,string类,集合,线程,时间,正则,流,jdk5--8各个版本的新特性,等等.不足的地方,欢迎大家补充. 源码分享:https: ...

  5. Python 函数的作用域

    python中的作用域有4种: 名称 介绍 L local,局部作用域,函数中定义的变量: E enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部作用域,但不是全局的: B ...

  6. 关于scanf与cin哪个快的问题

    一开始入c++的时候成天跑cin,cout 直到有一天用cin,cout超时 才知道scanf比cin快的多 但是后来又听说加了ios::sync_with_stdio(false);的cin跟飞一样 ...

  7. [VUE ERROR] Error in render: "TypeError: Cannot create property 'header' on boolean 'true'"

    项目基于ElemnetUi进行的开发,在引入第三方扩展库 vue-element-extends 之后使用它的表格组件报了这个错 解决方案: 1.  删除项目中的 node_modules 2. 删除 ...

  8. 我的Java之旅 第二课 Eclipse使用

    1.项目引用的jar包管理 在Project Explorer中找到你要添加jar包的项目,右键项目名,点击Properties. 在弹出的窗体中,点击Resource中的JAVA Build Pat ...

  9. python练习(-)

    简单的爬虫示例: import urllib.request #python2.x版本为urllib2url = 'http://www.douban.com/'webPage=urllib.requ ...

  10. tab 页形式展现多张报表

    业务系统中,很多报表都是沿用之前 EXCEL 的报表样式,原来以 sheet 格式显示的表,客户在 web 端展现的时候也希望也有同样的格式,润乾在实现这种效果和 EXCEL 一样简单灵活,轻松将数据 ...