您的位置:首页 > 编程语言 > Java开发

java-多线程1

2015-08-22 22:10 513 查看
一、创建多线程的方式1--继承Thread类

步骤:1自定义类MyThread继承Thread类 2MyThread里面重写run()方法 3创建对象 4启动线程

package test;

public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
for(int i=0;i<100;i++){
System.out.println(getName()+": "+i );
}
}
}


package test;

public class Test01 {
public static void main(String[] args) {
MyThread mt=new MyThread();
mt.start();
MyThread mt2=new MyThread();
mt2.start();

}
}


二、线程基础

1获得线程名称

getName()


2设置线程名称

my1.setName("线程1");


3返回当前正在执行的线程的名称

Thread.currentThread().getName();


4线程优先级

1-10,默认是5

获得优先级

my1.getPriority()


设置优先级

my1.setPriority(2);


三、线程控制

1线程睡眠

package test;

public class Test01 {
public static void main(String[] args) {
MyThread mt=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();

mt.setName("进程1");
mt2.setName("进程2");
mt3.setName("进程3");

mt.start();
mt2.start();
mt3.start();
}
}


package test;
import java.util.Date;
public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
for(int i=0;i<100;i++){
System.out.println(getName()+": "+i +"  " +new Date());
//睡眠
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


2线程加入

package test;

public class Test01 {
public static void main(String[] args) {
MyThread mt=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();

mt.setName("进程1");
mt2.setName("进程2");
mt3.setName("进程3");

mt.start();
//线程加入    该线程执行完毕,其他线程才能执行
try {
mt.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mt2.start();
mt3.start();
}
}


package test;
import java.util.Date;
public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
for(int i=0;i<100;i++){
System.out.println(getName()+": "+i +"  " +new Date());
//睡眠
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


3线程礼让

package test;

public class Test01 {
public static void main(String[] args) {
MyThread mt=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();

mt.setName("进程1");
mt2.setName("进程2");

mt.start();
mt2.start();

}
}


package test;
import java.util.Date;
public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
for(int i=0;i<100;i++){
System.out.println(getName()+": "+i +"  " +new Date());
//线程礼让
Thread.yield();
}
}
}


4守护线程 主线程挂了,守护线程都会挂掉

package test;

import java.util.Date;

public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
// super.run();
for (int i = 0; i < 20; i++) {
System.out.println(getName() + ": " + i + "  " + new Date());

}
}
}


package test;

public class Test01 {
public static void main(String[] args) {
MyThread mt=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();

mt.setName("进程1");
mt2.setName("进程2");

//设置守护线程
mt.setDaemon(true);
mt2.setDaemon(true);

mt.start();
mt2.start();

//改主线程的名字为“刘备”
Thread.currentThread().setName("刘备");
for(int x=0;x<5;x++){
System.out.println(Thread.currentThread().getName()+": "+x);
}

}
}


5进程中断

package test;

public class Test01 {
public static void main(String[] args) {
MyThread mt = new MyThread();

mt.start();
//进程中断
//超过3秒不醒,则中断
try {
Thread.sleep(3000);
mt.interrupt();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


package test;

import java.util.Date;

public class MyThread extends Thread {
@Override
public void run() {
System.out.println("开始执行:"+new Date());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("线程被终止了");
}
System.out.println("结束执行:"+new Date());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: