使Java线程停止的两种方法
2017-08-02 00:00
381 查看
package Thread;
public class testthread {
public static void main(String[] args) {
asdf t = new asdf();
Thread tt = new Thread(t,"线程1");
tt.start();
try { //主线程休眠10秒,
tt.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tt.interrupt(); //主线程打断.
}
}
class asdf implements Runnable{
public void run() {
int i = 1;
boolean b = true;
while(b){
System.out.println(Thread.currentThread().getName().toString()+i);
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
break;
}
}
}
}
改变while中的条件
package Thread;
public class testthread {
public static void main(String[] args) {
asdf t = new asdf();
Thread tt = new Thread(t,"线程1");
tt.start();
}
}
class asdf implements Runnable{
public void run() {
int i = 1;
boolean b = true;
while(b){
if(i==10){
b = false;
}
System.out.println(Thread.currentThread().getName().toString()+i);
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
}
}
public class testthread {
public static void main(String[] args) {
asdf t = new asdf();
Thread tt = new Thread(t,"线程1");
tt.start();
try { //主线程休眠10秒,
tt.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tt.interrupt(); //主线程打断.
}
}
class asdf implements Runnable{
public void run() {
int i = 1;
boolean b = true;
while(b){
System.out.println(Thread.currentThread().getName().toString()+i);
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
break;
}
}
}
}
改变while中的条件
package Thread;
public class testthread {
public static void main(String[] args) {
asdf t = new asdf();
Thread tt = new Thread(t,"线程1");
tt.start();
}
}
class asdf implements Runnable{
public void run() {
int i = 1;
boolean b = true;
while(b){
if(i==10){
b = false;
}
System.out.println(Thread.currentThread().getName().toString()+i);
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
}
}
相关文章推荐
- Java中两种停止线程的方法
- Java判断线程是否是停止状态的两种方法
- JAVA线程之一:实现一个线程的两种方法
- java中创建线程的两种方法及其区别
- Java创建线程的两种方法
- Java创建线程的两种方法
- JAVA中创建线程对象的两种方法
- 创建Java线程的两种方法
- java停止线程的方法
- java里面开启一个新的线程有两种方法
- java线程的两种创建方法
- java基础学习__多线程(停止线程,守护线程,join方法)
- Java创建线程的两种方法
- Java多线程初学者指南(8):从线程返回数据的两种方法
- java学习日记(5)——创建线程的两种基本方法
- 开发笔记:创建Java线程的两种方法
- Java多线程初学者指南(8):从线程返回数据的两种方法
- 初学Java多线程:从线程返回数据的两种方法
- Java多线程初学者指南(8):从线程返回数据的两种方法
- Java创建线程的两种方法