--------------------实现线程的两种方式-----------------
package com.tangkuo.thread.cn;
/**
*
* @author tangkuo
*
*/
public class ThreadDemo extends Thread{
/**
* 线程 每次输出一个数字,休眠5秒钟
* 打印当前线程的名字与输出的数字值
*
*/
@Override
public void run(){
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("新线程"+ Thread.currentThread().getName()+"输出"+i);
}
}
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
thread1.start();
thread2.start();
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程"+ Thread.currentThread().getName()+"输出"+i);
}
}
}
package com.tangkuo.thread.cn;
public class RunnableDemo implements Runnable {
@Override
public void run() {
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("新线程"+ Thread.currentThread().getName()+"输出"+i);
}
}
public static void main(String[] args) {
RunnableDemo runnableDemo = new RunnableDemo();
//Runnable runnableDemo = new RunnableDemo();
Thread thread1 = new Thread(runnableDemo);
Thread thread2 = new Thread(runnableDemo);
thread1.start();
thread2.start();
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程的名字"+ Thread.currentThread().getName()+"输出值"+i);
}
}
}
package com.tangkuo.thread.cn;
/**
* 利用线程插队求和
* @author Administrator
*
*/
public class SumRunnableDemo implements Runnable {
private int x;
private int y;
private long sum;
@Override
public void run() {
for(int i = x; i < y; i++){
sum +=i;
}
}
public SumRunnableDemo(int x, int y) {
super();
this.x = x;
this.y = y;
System.out.println(x+"到"+y+"之间的和等于");
}
public long getSum() {
return sum;
}
public static void main(String[] args) {
SumRunnableDemo srd = new SumRunnableDemo(1,100);
Thread thread = new Thread(srd);
thread.start();
//线程插队求和
try {
thread.join();//主线程必须等子线程执行完毕才能运行关闭。保证子线程所有业务逻辑处理完成
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sum="+srd.getSum());
}
}
package com.tangkuo.thread.cn;
/**
*
* @author tangkuo
*
*/
public class ThreadDemo extends Thread{
/**
* 线程 每次输出一个数字,休眠5秒钟
* 打印当前线程的名字与输出的数字值
*
*/
@Override
public void run(){
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("新线程"+ Thread.currentThread().getName()+"输出"+i);
}
}
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
thread1.start();
thread2.start();
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程"+ Thread.currentThread().getName()+"输出"+i);
}
}
}
package com.tangkuo.thread.cn;
public class RunnableDemo implements Runnable {
@Override
public void run() {
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("新线程"+ Thread.currentThread().getName()+"输出"+i);
}
}
public static void main(String[] args) {
RunnableDemo runnableDemo = new RunnableDemo();
//Runnable runnableDemo = new RunnableDemo();
Thread thread1 = new Thread(runnableDemo);
Thread thread2 = new Thread(runnableDemo);
thread1.start();
thread2.start();
for(int i = 0; i < 10; i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程的名字"+ Thread.currentThread().getName()+"输出值"+i);
}
}
}
package com.tangkuo.thread.cn;
/**
* 利用线程插队求和
* @author Administrator
*
*/
public class SumRunnableDemo implements Runnable {
private int x;
private int y;
private long sum;
@Override
public void run() {
for(int i = x; i < y; i++){
sum +=i;
}
}
public SumRunnableDemo(int x, int y) {
super();
this.x = x;
this.y = y;
System.out.println(x+"到"+y+"之间的和等于");
}
public long getSum() {
return sum;
}
public static void main(String[] args) {
SumRunnableDemo srd = new SumRunnableDemo(1,100);
Thread thread = new Thread(srd);
thread.start();
//线程插队求和
try {
thread.join();//主线程必须等子线程执行完毕才能运行关闭。保证子线程所有业务逻辑处理完成
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sum="+srd.getSum());
}
}