Thread(上) 【015】

//、、、、、[color=red]创建线程的方法一[/color]、、、、、、、、、、、、、、、、、、、\\

[code]
package com.testthread;

public class TestThread2 {
public static void main(String args[]) {
Runner2 r = new Runner2() ;
r.start() ;
for(int i=0; i<100; i++) {
System.out.println("Main thread:-----" + i) ;
}
}
}
class Runner2 extends Thread { //通过继承Thread 类创建线程
public void run() {
for(int i=0; i<30; i++) {
System.out.println("Runner2:--" + i) ;
}
}
}
[/code]

//、、、、、[color=red]创建线程的方法二[/color]、、、、、、、、、、、、、、、、、、、\\
[code]
package com.testthread; //默认导入java.lang包
public class TestThread1 {
public static void main(String args[]) {
Runner1 r = new Runner1() ;
Thread th = new Thread(r) ;
th.start(); //线程启动
r.run(); //这个是方法调用,不能使新线程启动

for(int i=0; i<100; i++) {
System.out.println("Main thread:-----" +i) ;
}
}
}
class Runner1 implements Runnable { // 通过实现runnable接口创建和启动线程,尽量使用该方法而少用(不用)继承Thread方法
public void run() {
for(int i=0; i<30; i++) {
System.out.println("Runner1------"+i) ;
}
}
}
[/code]
//。。。。。[color=red]TestInterrupted[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;
import java.util.* ;
public class TestInterrupted {
public static void main(String args[]) {
Thread th = new Thread(new MyThread()) ;
th.start();
try {
Thread.sleep(10000) ; //该sleep 是让Main 线程休眠10秒,让th 线程工作
} catch (InterruptedException e) {
e.printStackTrace();
}
th.interrupt(); //中断线程,MyThread 里的catch
}
}
class MyThread implements Runnable {
public void run() {
while(true) {
System.out.println("===" + new Date() +"===") ; //输出系统时间,输出十次,因为main休眠10秒
try {
Thread.sleep(1000); // sleep 是static 属性,可以直接调用。让该线程休眠1秒
} catch (InterruptedException e) {
return; // 当休眠被打断时停止
}
}
}
}
[/code]

//。。。。。[color=red]TestJoin[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;

public class TestJoin {
public static void main(String[] args) {
MyThread1 m = new MyThread1("New") ;
m.start();
try {
m.join(); //等待m线程终止才继续往下进行,和调用run方法结果一样
} catch (InterruptedException e) {
e.printStackTrace();
}

for(int i=1; i<=10; i++) {
System.out.println("This is main thread.") ;
}
}
}
class MyThread1 extends Thread {
MyThread1(String s) {
super(s) ; //调用Thread中的构造方法
}

public void run() {
for(int i=1; i<=10; i++) {
System.out.println("This is" + getName()); //返回该线程的名称
try {
sleep(1000) ;
} catch (InterruptedException e) {
return ;
}
}
}
}
[/code]


//。。。。。[color=red]TestPriority[/color]。。。。。。。。。。。。。。。。。。\\
[code]
package com.testthread;

public class TestPriority {
public static void main(String args[]) {
Thread t1 = new Thread(new MyThread3()) ;
Thread t2 = new Thread(new MyThread4()) ;
t1.setPriority(Thread.NORM_PRIORITY + 3) ; //Thread.NORM_PRIORITY---分配给线程的默认优先级
t1.start() ;
t2.start() ;
}
}

class MyThread3 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("MyThread3: " + i) ;
}
}
}

class MyThread4 implements Runnable {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("-------Mythreads4: " + i) ;
}
}
}
[/code]

//。。。。。[color=red]TestStop[/color]。。。。。。。。。。。。。。。。。。\\

[code]
package com.testthread;

public class TestStop {
public static void main(String args[]) {
ThreadRun tr = new ThreadRun() ;
Thread r = new Thread(tr);
r.start();
for (int i = 0; i < 1000000; i++) {
if (i % 1000 == 0) {
System.out.println("in thread main i=" + i);
}
}
System.out.println("Thread main is over");
tr.shutDown() ;
r.stop();
}
}

class ThreadRun implements Runnable {
private boolean flag = true;

public void run() {
int i = 0 ;
while (flag = true) {
System.out.print(" " + i++);
}
}

public void shutDown() {
flag = false ;
}
}

[/code]

//。。。。。[color=red]TestYield[/color]。。。。。。。。。。。。。。。。。。\\

[code]
package com.testthread;
public class TestYield {
public static void main(String args[]) {
MyThread2 th1 = new MyThread2("th1") ;
MyThread2 th2 = new MyThread2("th2") ;
th1.start();
th2.start();
}
}

class MyThread2 extends Thread {
public MyThread2(String s) {
super(s) ;
}

public void run() {
for(int i=1; i<=100; i++) {
System.out.println(getName()+ ": "+i) ;
if(i%10 == 0) {
yield() ; //当i值是10的倍数时候,暂停当前正在执行的线程对象,并执行其他线程
}
}
}
}

[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值