星空java_java多线程

7b4afa989c5bef46093f42a664a8ed22.png

import java.util.*;

import java.text.*;

public class TestThread3 {

public static void main(String args[]) {

Runner r1 = new Runner(1);

Thread t1 = new Thread(r1);

Thread t2 = new Thread(r1);

Thread t3 = new Thread(r1);

Runner r2 = new Runner(2);

Thread t4 = new Thread(r2);

Thread t5 = new Thread(r2);

Thread t6 = new Thread(r2);

Timer timer = new Timer();

Thread t7 = new Thread(timer);

t1.start();

t2.start();

t3.start();

t4.start();

t5.start();

t6.start();

t7.start();

}

}

class Runner implements Runnable {

int id;

Runner(int id){

this.id = id;

}

public void run() {

int i=0;

while( true ){

i++;

System.out.println("ID: " + id + " No. " + i);

try{ Thread.sleep(300); } catch( InterruptedException

e ){}

}

}

}

class Timer implements Runnable {

public void run(){

while(true){

System.out.println( new SimpleDateFormat().

format( new Date()));

try{ Thread.sleep(1000); } catch( InterruptedException

e ){}

}

}

}

f9261df15aaef0901436f93fc04eaf6f.png

msconfig

36ad1f634fa6a9af27af0298b2a67a1e.png

592e050f5bb1b03ac12c72710eaa4491.png

172d538a08643b67fdf2479b2707b245.png

76813a78bee88dc817704a2943d91409.png

public class TestThreadPriority {

public static void main(String args[]) {

Thread t1 = new Thread( new Runner(1) );

Thread t2 = new Thread( new Runner(2) );

Thread t3 = new Thread( new Runner(3) );

t1.setPriority( Thread.MIN_PRIORITY );

t2.setPriority( Thread.NORM_PRIORITY );

t3.setPriority( Thread.MAX_PRIORITY );

t1.start();

t2.start();

t3.start();

}

}

class Runner implements Runnable {

int id;

Runner(int id){

this.id = id;

}

public void run() {

for(int i=0; i<100; i++ ){

if( i % 100 == 0 ) System.out.print("\r");

Thread.currentThread().yield();

System.out.print(id);

}

}

}

49e78c2d493de6a97fc0572a6a1cb0a3.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值