多线程2

更新时间:2024-06-22 11:08:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

编写类ExceptionTest2: 定义两个方法:go()和main()

在go方法中声明要抛出异常,在该方法体内,抛出一个Exception对象

在main()方法中,调用go方法,使用try/catch捕获go方法中抛出的异常

class ExceptionTest2 { }

void go()throws Exception { }

throw(new Exception(\抛出的异常!\

public class Test { }

public static void main(String args[]) { }

ExceptionTest2 t2=new ExceptionTest2(); try { }

catch (Exception e) { }

e.printStackTrace(); t2.go();

实现多线程的两种方法: 继承Thread类

实现Runnable接口

先看继承Thread

class MyThread extends Thread { }

public class Test {

public static void main(String args[]) {

MyThread t1=new MyThread(); MyThread t2=new MyThread(); MyThread t3=new MyThread(); t1.start(); t2.start(); private int ticket=5; public void run() { }

for(int i=1;i<100;i++) { }

if(ticket>0)

System.out.println(\卖票:\

}

}

t3.start();

程序运行结果如下: 卖票:5 卖票:4 卖票:3 卖票:2 卖票:5 卖票:1 卖票:5 卖票:4 卖票:4 卖票:3 卖票:2

卖票:1 卖票:3 卖票:2 卖票:1

从程序中我们可以看到3个线程各自卖各自的5张票,并没有出现共享5张票的情况。因为每一个Thread对象都有一个ticket=5.

用Runnable接口

class MyThread implements Runnable {

private int ticket=5; public void run() {

for(int i=1;i<100;i++)

}

}

{ }

if(ticket>0)

System.out.println(\卖票:\

public class Test { }

public static void main(String args[]) { }

MyThread th1=new MyThread(); Thread t1=new Thread(th1); Thread t2=new Thread(th1); Thread t3=new Thread(th1); t1.start(); t2.start(); t3.start();

程序运行结果如下: 卖票:5

卖票:2 卖票:1 卖票:3 卖票:4

我们可以看到3个线程共享这5张票。因为只创建了一个MyThread对象。因此采用接口可以实现资源共享。

线程的方法:

Thread.sleep():休眠

Thread.currentThread():获取当前线程

Thread.getName():获取线程的名字

Thread.currentThread().getName():获取当前线程的名字

构造方法

Thread(Runnable target,String name)

获取当前线程名字举例:

class MyThread implements Runnable {

public void run() {

for(int i=1;i<10;i++) {

System.out.println(Thread.currentThread().getName()+\运行\次\ } } }

public class Test {

public static void main(String args[]) {

MyThread th1=new MyThread(); Thread t1=new Thread(th1,\线程A\ Thread t2=new Thread(th1,\线程B\ Thread t3=new Thread(th1,\线程C\ t1.start(); t2.start(); t3.start(); } }

运行结果如下: 线程A运行1次

线程C运行1次 线程C运行2次 线程C运行3次 线程C运行4次 线程C运行5次 线程C运行6次 线程C运行7次 线程C运行8次 线程C运行9次 线程B运行1次 线程B运行2次 线程B运行3次 线程B运行4次 线程B运行5次 线程B运行6次

线程B运行7次 线程B运行8次 线程B运行9次 线程A运行2次 线程A运行3次 线程A运行4次 线程A运行5次 线程A运行6次 线程A运行7次 线程A运行8次 线程A运行9次

使用

Thread.currentThread().getName()方法,修改卖票程

序,显示出哪个线程在卖第几张票。三个线程的名字分别是”A”, ”B”, ”C”

class MyThread implements Runnable {

private int ticket=5; public void run() {

System.out.println(Thread.currentThread().getName()+\卖第for(int i=1;i<10;i++) {

if(ticket>0)

\张票\ }

public class Test {

public static void main(String args[]) { } }

}

}

MyThread th1=new MyThread(); Thread t1=new Thread(th1,\Thread t2=new Thread(th1,\Thread t3=new Thread(th1,\t1.start(); t2.start(); t3.start();

运行结果如下: A卖第5张票 C卖第3张票 C卖第2张票 B卖第4张票 A卖第1张票

在java的线程操作中,所有的线程在运行前都会保持在就绪状态,那么此时,哪个线程的优先级高,哪个线程就有可能会被先执行。 设置优先级:

void setPriority(int newPriority) setPriority(Thread.MIN_PRIORITY) ; // 优先级最低

setPriority(Thread.MAX_PRIORITY) ; // 优先级最高

setPriority(Thread.NORM_PRIORITY) ; // 优先级正常

class MyThread implements Runnable {

private int ticket=100;

public void run() {

System.out.println(Thread.currentThread().getName()+\卖第for(int i=1;i<1000;i++) {

if(ticket>0)

\张票\ }

public class Test {

public static void main(String args[]) {

MyThread th1=new MyThread(); Thread t1=new Thread(th1,\Thread t2=new Thread(th1,\Thread t3=new Thread(th1,\t1.setPriority(Thread.MIN_PRIORITY); t2.setPriority(Thread.MAX_PRIORITY); t3.setPriority(Thread.NORM_PRIORITY); } }

}

}

t1.start(); t2.start(); t3.start();

加入优先级以后我们发现优先级高的线程会优先运行。

优先级一共分为10级 Thread.MIN_PRIORITY=1 Thread.MAX_PRIORITY=10

Thread.NORM_PRIORITY=5

没有设置优先级时,默认是5级

猜数字:

随机给你一个数,猜猜是多少?

我第1次猜这个数是:50 你猜大了

我第2次猜这个数是:25 你猜大了

我第3次猜这个数是:12 你猜小了

我第5次猜这个数是:18 你猜小了

我第6次猜这个数是:21

你猜小了

我第7次猜这个数是:23 你猜小了

我第8次猜这个数是:24 恭喜,你猜对了 我成功了!

本文来源:https://www.bwwdw.com/article/sjd3.html

Top