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

java学习之线程的操作方法

2016-01-23 23:33 330 查看
package com.gh.thread;

/**
* 线程实现的两种方法
* 1、继承thread类
* 2、实现Runnable接口
* @author ganhang
*
*/
public class ThreadDemo {

public static void main(String[] args) {
//第一种线程实现
Mythread mythread =new Mythread();
mythread.setName("自定义线程1");
mythread.start();
try {
Thread.sleep(2);//当前线程睡眠2毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
//第二种线程实现
Thread thread=new Thread(new Mythread2(),"自定义线程2");
thread.start();
}
}

class Mythread extends Thread{
@Override
public void run() {
for (int i = 0; i < 3; i++) {
//输出当前线程的名字和i;
System.out.println(Thread.currentThread().getName()+"--"+i);
}
}
}
class Mythread2 implements Runnable{

@Override
public void run() {
for (int i = 0; i <3; i++) {
System.out.println(Thread.currentThread().getName()+"--"+i);
}
}

}


线程的同步

package com.gh.thread;
/**
* 同步实现顺序上厕所,假设厕所只有一个
* 1、使用synchronized同步代码块
* 2、使用同步方法
* @author ganhang
*/
public class ThreadDemo1 {
public static void main(String[] args) {
Mythread5 my=new Mythread5();
//注意这里必须使用同一个Mythread类的对象,不然无法同步
Thread t1=new Thread(my,"小白");//线程1
Thread t2=new Thread(my,"小黑");//线程2
//两个线程同时跑
t1.start();//小白要上厕所
t2.start();//小黑也要上厕所
}
}
class Mythread5 implements Runnable{
@Override
public void run() {
//同步代码块
synchronized (this) {//锁定对象一次只能进一个个
System.out.println(Thread.currentThread().getName()+"正在上厕所。。");
try {
Thread.sleep(1000);//模拟**。。。耗时1s
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"上完了。。");
}
}
}


中断线程

package com.gh.thread;

/**
* 中断线程
* 1、interrupt()方法
* 2、自定义标记完成中断
* @author ganhang
*
*/
public class ThreadDemo2 {
public static void demo1() {
Thread t1 = new Thread(new Mythread3(), "线程1");
t1.start();
for (int i = 0; i < 10; i++) {
if (i == 5)
t1.interrupt();//只是个标记中断不会真正中断,是否中断还是由线程自己决定
System.out.println("main-" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void demo2(){
Mythread4 my=new Mythread4();
Thread t1=new Thread(my,"线程2");
t1.start();
for(int i=0;i<10;i++){
if(i==5){
my.setFlag(true);//中断
}
System.out.println(Thread.currentThread().getName()+"--"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
demo1();//第一种方式实现
demo2();//第二种方式实现
}
}

class Mythread4 implements Runnable {
private boolean flag = false;

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

@Override
public void run() {
int i=0;
while (!flag) {
System.out.println(Thread.currentThread().getName() + "--" + i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
}

class Mythread3 implements Runnable {

public void run() {
int i = 0;
while (!Thread.interrupted()) {
System.out.println(Thread.currentThread().getName() + "--" + i++);
try {
Thread.sleep(1000);// sleep会清除interrupt状态
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: