public class Thread_one {
public static void main(String [] args){
Run run = new Run();
//run.run();//此为方法的调用,和线程有着天壤之别
Thread thread = new Thread(run);
thread.start();//启动线程,调用线程的run()方法
for(int i=1; i<=20; i++){
System.out.println("主线程i的 值:--------"+i);
}
}
}
class Run implements Runnable{
@Override
public void run() {
for(int i=1; i<=20; i++){
System.out.println("子线程i的 值:"+i);
}
}
}
public class Thread_sleep {
/*
* 线程停顿
*/
public static void main(String [] args){
Runone run = new Runone();
Thread thread = new Thread(run);
thread.start();
try {
Thread.sleep(5000);
thread.interrupt();//中断线程的执行
//thread.stop();//相对中断线程,stop过于粗暴,不建议使用,一般在需要强制关闭子线程时方使用此方法
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Runone implements Runnable{
@Override
public void run() {
for(int i=1 ; i<10; i++){
try {
Thread.sleep(1000);
System.out.println("-----"+new Date()+"-----");
} catch (InterruptedException e) {
return ;//当捕获到子线程被中断时,直接关闭子线程
}
}
}
}
public class Thread_stop {
public static void main(String [] args){
Runthree run = new Runthree();
Thread th = new Thread(run);
th.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
run.setStop();
}
}
class Runthree implements Runnable{
boolean flag;
@Override
public void run() {
flag = true;
int i = 0;
while(flag){
try {
System.out.println("子线程----"+(i++));
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void setStop(){
flag = false;
}
}
public class Thread_join {
/*
* 合并线程
*/
public static void main(String [] args){
Runtwo run = new Runtwo();
Thread thread = new Thread(run);
thread.start();
try {
thread.join();//合并线程,此时相当于方法调用
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0; i<10; i++){
System.out.println("主线程:"+i);
}
}
}
class Runtwo implements Runnable{
@Override
public void run() {
for(int i=0; i<10; i++){
System.out.println("子线程:----"+i);
}
}
}
public class Thread_yield {
/**让出CPU
* @param args
*/
public static void main(String[] args) {
Th th = new Th("aaa");
th.start();
for(int i = 0 ; i<=10; i++){
System.out.println("主线程----"+i);
}
}
}
class Th extends Thread{
Th(){}
Th(String s){super(s);}
@Override
public void run() {
for(int i = 0; i<=10; i++){
if(i%3!=0){
System.out.println("子线程"+i);
}else{
System.out.println("子线程i="+i+" 线程进行切换");
yield();//从Thread继承方可使用此方法
}
}
}
}
public class Thread_priority {
/*
* priority设置线程的优先级
* Thread默认的优先级为5;Thread的最大优先级为10,最小为0
*/
public static void main(String [] args){
T1 t1 = new T1();
T2 t2 = new T2();
t1.start();
//t1.setPriority(Thread.NORM_PRIORITY+3);//设置t1的优先级
t2.start();
}
}
class T1 extends Thread{
@Override
public void run() {
for(int i = 0; i<50; i++){
System.out.println("线程T1-----"+i);
}
}
}
class T2 extends Thread{
@Override
public void run() {
for(int i = 0; i<50; i++){
System.out.println("线程T2"+i);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有