初始线程
package com.xiancheng.ch01;
public class ThreadTest01 {
public static void main(String[] args) throws InterruptedException {
//获得当前线程
Thread t1 = Thread.currentThread();
System.out.println("Threadname:"+t1.getName());
//获得ID
System.out.println("ThreadID:"+t1.getId());
//获取优先级
System.out.println("Thread优先级:"+t1.getPriority());
//Thread.sleep(1000);//参数的单位是毫秒
for (int i = 1; i <=10; i++) {
System.out.println(i);
Thread.sleep(1000);
}
System.out.println("我睡眠了");
}
}
方法不运用线程的时候,程序像一根筋一样的走
package com.xiancheng.ch01;
/*
* 开发线程的一个类,首先要求继承java.lang.Threa
* 重写其中的run()方法
*/
//方法不运用线程就会变成整体就变成单一线程,先执行方法,等方法执行完告诉mian.main才执行
public class ThreadTest7 {
public void run() {
for (int i = 1; i <=10; i++) {
//currentThread()当前线程
System.out.println("run:"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadTest7 tt2 =new ThreadTest7();
tt2.run();
for (int i = 1; i <=10; i++) {
System.out.println("main"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
方法运用线程的时候,嘿,好兄弟我们一起走
package com.xiancheng.ch01;
/*
* 开发线程的一个类,首先要求继承java.lang.Threa
* 重写其中的run()方法
*/
//运用了线程,main方法就可以和线程一起执行,不用等到线程执行完毕结束main方法才接下来运行
public class ThreadTest6 extends Thread {
//父类没有抛异常,子类不能抛异常,这次只能捕获
@Override
public void run() {
for (int i = 1; i <=10; i++) {
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadTest6 tt2 =new ThreadTest6();
//join合并得在start之后
tt2.start();
// new Thread(tt2).start();
// new Thread(tt2).start();
for (int i = 1; i <=10; i++) {
//当main方法读到这句话的时候,告诉tt2我们合并在一起
//这时候合并线程,将tt2线程合并到了main线程,变成了一个线程
// tt2.join();
System.out.println("main"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
可以看出main老板调用线程的时候我是执行我线程方法,main老板懒得理我完成怎样了。这也不难举个例子,比如你QQ开空间的时候加载空间里面的内容和你点QQ头像聊天,你聊天的时候非得等你空间加载完成告诉我空间加载完成了吗?没必要,你加载你的空间,我聊我的天。别耽误我们的时间,其实线程也不难看出是统筹简算的成果。
package com.xiancheng.ch01;
public class ThreadTest01 {
public static void main(String[] args) throws InterruptedException {
//获得当前线程
Thread t1 = Thread.currentThread();
System.out.println("Threadname:"+t1.getName());
//获得ID
System.out.println("ThreadID:"+t1.getId());
//获取优先级
System.out.println("Thread优先级:"+t1.getPriority());
//Thread.sleep(1000);//参数的单位是毫秒
for (int i = 1; i <=10; i++) {
System.out.println(i);
Thread.sleep(1000);
}
System.out.println("我睡眠了");
}
}
第一种继承Thread类
package com.xiancheng.ch01;
/*
* 开发线程的一个类,首先要求继承java.lang.Threa
* 重写其中的run()方法
*/
//运用了线程,main方法就可以和线程一起执行,不用等到线程执行完毕结束main方法才接下来运行
public class ThreadTest6 extends Thread {
//父类没有抛异常,子类不能抛异常,这次只能捕获
@Override
public void run() {
for (int i = 1; i <=10; i++) {
//currentThread()当前线程
System.out.println(Thread.currentThread().getName()+":"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadTest6 tt2 =new ThreadTest6();
//如果你写成tt2.run()是启动线程执行run方法
//而不是启动线程,启动线程用tt2.start
tt2.start();
for (int i = 1; i <=10; i++) {
System.out.println("main"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
多次调用线程的方法一
多次调用线程的方法二
第二种 实现Runnable接口
package com.xiancheng.ch01;
/*
* 线程的实现 方式二
* 实现runnable接口,实现其中run()方法
* 如何启动这个线程?
*/
public class ThreadTest8 implements Runnable {
@Override
public void run() {
for (int i = 1; i <=10; i++) {
System.out.println("run:"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ThreadTest8 tt3 =new ThreadTest8();
new Thread(tt3).start();
for(int i=1;i<=10;i++) {
System.out.println("main : "+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Runable接口多次调用线程方法
拓展补充线程创建的另外两种方法
方法一:实现Callable接口
代码
package com.UserMangertest01;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/*
* JDK5.0新增的线程的实现方式:
* 实现方式三:
* > 实现这个接口:Callable,实现call方法
* > 创建FutureTask实例,通过线程实现类构造完成的
* > 通过Thread类的实例来启动线程
* > 通过 task.get();获取返回值
*
*/
public class ThreadTest111 implements Callable {
int sum =0;
@Override
public Object call() throws Exception {
//完成1-100累加和
for (int i = 1; i <=100; i++) {
sum+=i;
System.out.println("sum:"+sum);
Thread.sleep(20);
}
return sum;
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ThreadTest111 tt1 =new ThreadTest111();
//没有直接调用的方法,这里得先创建一个未来的任务
FutureTask task =new FutureTask(tt1);
//再去执行未来任务里面的内容
new Thread(task).start();
//不过能获取返回值
Object str =task.get();
System.out.println(str);
//总结,我觉得Callable接口实现线程步骤挺复杂的,不过它有renturn返回值这一功能,怎么说有的也有失吧
}
}
方法二 :线程池方法
newFixedThreadPool() 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
代码
package com.UserMangertest01;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExcutorTest1 {
public static void main(String[] args) {
//线程池的演示
ExecutorService threadPool = Executors.newFixedThreadPool(100);
//for循环是为了创造多对线程,轻松快速
for (int i = 0; i < 30; i++) {
threadPool.execute(new MyThread101() );
threadPool.execute(new MyThread102() );
}
// threadPool.execute(new MyThread101() );
// threadPool.execute(new MyThread102() );
//用完记得关闭线程池
threadPool.shutdown();
//总结,虽然线程池的名字很长,但是我感觉真的很好用
}
}
class MyThread101 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(i%2==0) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class MyThread102 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(i%2!=0) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
中断线程的操作
package com.xiancheng.ch01;
/*
* 线程的实现 方式二
* 实现runnable接口,实现其中run()方法
* 如何启动这个线程?
*/
public class ThreadTest8 implements Runnable {
//每个线程都有flag,标志着中断位
/*
* 因为服务或线程不能被立即停止,立即停止会使共享的数据结构不一致,相反,应该在停止前做一些清理工作,然后再结束。
* 所以说,不能你让我停我就停,我自己执行的任务,我比你更能清楚在停止前如何进行清理工作。
* 因此,最终的设计就变成了:线程main给tt3发interrupt信号,B收到信号后,自己决定先做些什么,然后再退出。
* 这是一种协作机制,需要tt3配合。
*/
private boolean flag; //默认为false没有定义的时候
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
for (int i = 1; i <=10; i++) {
System.out.println("run:"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(flag) {
//tt3检查自己的interrupt状态为true,并自愿地退出线程,是Java中唯一的一个线程让另一个线程终止的方法!
return;
}
// if(i==5) {
// //当i==5的时候终止线程的演示 //不是最佳选择
// Thread.currentThread().interrupt();//java.lang.InterruptedException
// }
}
}
public static void main(String[] args) {
ThreadTest8 tt3 =new ThreadTest8();
new Thread(tt3).start();
for(int i=1;i<=10;i++) {
System.out.println("main : "+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i==5) {
//如果main方法想让tt3这个线程结束,就把这个中断位flag设为true
//终止tt3这个线程
tt3.setFlag(true);
}
}
}
}
方法一
package com.xiancheng.ch01;
public class ThreadTest9 {
public static void main(String[] args) {
Nunoushu p1 =new Nunoushu();
Nunjishu p2 =new Nunjishu();
p1.start();
p2.start();
}
}
class Nunoushu extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 == 0) {
System.out.println(this.getName()+" --> "+i);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Nunjishu extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 != 0) {
System.out.println(this.getName()+" --> "+i);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
方法二(用匿名类)
package com.xiancheng.ch01;
public class ThreadTest10 {
public static void main(String[] args) {
//匿名类的奇数
new Thread() {
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 != 0) {
System.out.println(this.getName()+" --> "+i);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
//匿名类的偶数
new Thread() {
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 == 0) {
System.out.println(this.getName()+" --> "+i);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
有的人觉得输出名字很丑在控制台上 可以修改名字
方法一
方法二
方法三
调用父类String的方法
代码
package com.xiancheng.ch01;
public class ThreadTest9 {
public static void main(String[] args) {
Nunoushu p1 =new Nunoushu("偶数");
Nunjishu p2 =new Nunjishu("奇数");
p1.start();
//方法一
// p1.setName("偶数");
// p2.setName("奇数");
p2.start();
}
}
class Nunoushu extends Thread {
//方法二 在每一个自己的类里面构造
public Nunoushu(String name) {
//方法三
//因为父类中存在有
// public Thread(String name)
//可以重写父类的构造方法
super(name);
}
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 == 0) {
//当super父类的时候,所用的方法是父类,
//父类里面有 public final synchronized void setName(String name)
System.out.println(this.getName()+" --> "+i);
}
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Nunjishu extends Thread {
//方法三 继承父类的构造方法改写
public Nunjishu(String name) {
super(name);
}
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 != 0) {
System.out.println(this.getName()+" --> "+i);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.xiancheng.ch01;
public class ThreadTest9 {
public static void main(String[] args) {
Nunoushu p1 =new Nunoushu("偶数");
Nunjishu p2 =new Nunjishu("奇数");
p1.setPriority(Thread.MAX_PRIORITY);
p1.start();
System.out.println("-------------------->>>>> "+p1.isAlive());
//设置优先级必须在启动前面,不能给负数或者给小于最小优先级 不然会报错。
p2.setPriority(Thread.MIN_PRIORITY);
p2.start();
System.out.println("-------------------->>>>> "+p2.isAlive());
}
}
class Nunoushu extends Thread {
public Nunoushu(String name) {
super(name);
}
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 == 0) {
System.out.println(this.getName()+" --> "+i);
}
}
}
}
class Nunjishu extends Thread {
public Nunjishu(String name) {
super(name);
}
public void run() {
for (int i = 1; i <= 100; i++) {
if(i % 2 != 0) {
System.out.println(this.getName()+" --> "+i);
}
}
}
}