1.1.通过继承Thread类实现多线程
public class ThreadTest extends Thread{
/*
1.首先需要继承Thread类
2.重写run方法
3.实例化对象,调用start()方法开启多线程
*/
@Override
public void run() {
//这里是执行多线程的函数体
for (int i = 0; i < 200; i++) {
System.out.println("我在看代码"+i);
}
}
public static void main(String[] args) {
ThreadTest threadTest = new ThreadTest();
threadTest.start();
for (int i = 0; i < 1000; i++) {
System.out.println("我在学习多线程"+i);
}
}
}
1.2下载图片
public class ThreadTest2 extends Thread{
private String url;
private String name;
public ThreadTest2(String url ,String name){
this.url = url;
this.name = name;
};
@Override
public void run() {
WebDownLoader webDownLoader = new WebDownLoader();
webDownLoader.download(url,name);
System.out.println("下载完成:"+name);
}
public static void main(String[] args) {
ThreadTest2 t1 = new ThreadTest2("https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1906469856,4113625838&fm=26&gp=0.jpg","1.jpg");
ThreadTest2 t2 = new ThreadTest2("https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1141259048,554497535&fm=26&gp=0.jpg","2.jpg");
ThreadTest2 t3 = new ThreadTest2("https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1830914723,3154965800&fm=26&gp=0.jpg","3.jpg");
t1.start(); //third
t2.start(); //first
t3.start(); //second
}
}
class WebDownLoader{
public void download(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.1通过实现Runnable接口实现多线程(建议使用)
public class ThreadTest3 implements Runnable{
@Override
public void run() {
//这里是执行多线程的函数体
for (int i = 0; i < 200; i++) {
System.out.println("我在看代码"+i);
}
}
public static void main(String[] args) {
ThreadTest3 threadTest3 = new ThreadTest3();
new Thread(threadTest3).start();
for (int i = 0; i < 1000; i++) {
System.out.println("我在学习多线程"+i);
}
}
}
继承Thread 避免OOP单继承的局限性
实现Runnable 方便被多个线程调用
关于线程停止
不建议使用stop()等jdk不推荐的方法
建议自己写一个停止函数
public class testStop implements Runnable {
private boolean flag = true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("线程在执行..."+i++);
}
}
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
testStop testStop = new testStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("for循环"+i);
if (i==900){
testStop.stop();
System.out.println("第"+i+"次,线程该停止了");
}
}
}
}
关于线程休眠_sleep()
通过Thread.sleep()方法来实现
线程礼让_yield()
通过Thread.yield()方法来实现
让CPU重新调度,礼让不一定成功!看CPU心情。
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程执行结束");
}
}
线程强制执行_join()
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程VIP来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
for (int i = 0; i < 300; i++) {
if (i==200){
thread.join();
}
System.out.println("main线程"+i);
}
}
}
监测线程状态
实例化线程对象后,调用getState()方法。
线程优先级
使用以下方式改变或获取优先级
getPriority() .setPriority(int xxx)
Thread.MIN_PRIORITY = 1
Thread.MAX_PRIORITY = 10
Thread.NORM_PRIORITY = 5
守护线程
new Thread().setDaemon(true) //默认是false
线程锁
同步方法:方法上加synchornized
同步代码块:synchornized(Obj){}
Lock可重入锁
使用步骤:
1.实例化ReentrantLock 对象
2.用trycatch捕获异常
3.关闭锁
public class TestLock {
public static void main(String[] args) {
testLock2 testLock2 = new testLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class testLock2 implements Runnable{
private int tickets=10;
private ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock(); //加锁
if (tickets>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(tickets--);
}else{
break;
}
}finally {
lock.lock(); //释放锁
}
}
}
}