目录
ThreadTest
package 线程;
public class ThreadTest {
public static void main(String[] args) {
Mythread s=new Mythread();
//s.run();//不会启动线程,不会分配新的分支栈,单线程
s.start();
for (int i=0;i<18;i++){
System.out.println("主线程"+i);
}
}
}
class Mythread extends Thread{
@Override
public void run() {
//运行在分支线程中
for (int i=0;i<10;i++){
System.out.println("分支线程"+i);
}
}
}


ThreadTest 01
package 线程;
public class ThreadTest01 {
public static void main(String[] args) {
//创建一个可运行的对象
// MyRunnable m=new MyRunnable();
//将可运行的对象封装成一个线程对象
//Thread s=new Thread(m);
Thread s=new Thread(new MyRunnable());//合并
s.start();
for(int i=0;i<11;i++){
System.out.println("主线程"+i);
}
}
}
class MyRunnable implements Runnable{//这并不是一个线程类,是一个可运行的类,还不是一个线程
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println("分支线程"+i);
}
}
} 


ThreadTest 02
package 线程;
//用匿名内部类
public class ThreadTest02 {
public static void main(String[] args) {
Thread t=new Thread(new Runnable() {//接口不能new对象 相当于new 【匿名】 implements Runnable
@Override
public void run() {
for(int i=0;i<11;i++){
System.out.println("分支线程"+i);
}
}
});
t.start();
for(int i=0;i<11;i++){
System.out.println("主线程"+i);
}
}
}

ThreadTest 03
package 线程;
public class ThreadTest03 {
public static void main(String[] args) {
Mythread2 s=new Mythread2();
s.setName("小明");
System.out.println(s.getName());
s.start();
}
}
class Mythread2 extends Thread{
@Override
public void run() {
for(int i=0;i<11;i++){
Thread t=Thread.currentThread();
System.out.println(t.getName());
System.out.println("分支线程"+i);
}
}
}

ThreadTest 04
package 线程;
public class ThreadTest04 {
public static void main(String[] args) {
Thread t=Thread.currentThread();
t.setName("嘿嘿");
for(int i=0;i<10;i++){
try {
Thread.sleep(1000*2);//等两秒
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(t.getName()+":"+i);
}
}
}


ThreadTest 05
package 线程;
public class ThreadTest05 {
public static void main(String[] args) {
Thread s=new Thread(new Mythread3());
s.setName("名字");
s.start();
try {
Thread.sleep(1000*2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
s.interrupt();
}
}
class Mythread3 implements Runnable{
@Override
public void run() {
Thread t=Thread.currentThread();
System.out.println(t.getName()+"begin");
try {
Thread.sleep(1000*60*60*24*365);//睡一年
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("睡醒了");
}
}

ThreadTest 06
package 线程;
//正确的终止线程
public class ThreadTest06 {
public static void main(String[] args) {
Mythread5 m = new Mythread5();
Thread t = new Thread(m);
t.setName("小孩");
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
m.run=false;//终止线程
}
}
class Mythread5 implements Runnable {
boolean run=true;
@Override
public void run() {
for (int i=0;i<10;i++) {
if(run==true){
System.out.println(Thread.currentThread().getName() + "--" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}else {
//这里可以写要保存的地方东西,终止当前线程
return;
}
}
}
} 

ThreadTest 07
package 线程;
//线程的优先级
public class ThreadTest07 {
public static void main(String[] args) {
/* System.out.println("最高优先级:"+Thread.MAX_PRIORITY);
System.out.println("默认优先级:"+Thread.NORM_PRIORITY);
System.out.println("最低优先级:"+Thread.MIN_PRIORITY);*/
Thread.currentThread().setPriority(1);//设置主线程的优先级为1
Thread t=new Thread(new Mythread9());
t.setPriority(10);//设置名字线程优先级为10
t.setName("名字");
t.start();
for(int i=0;i<500;i++){
System.out.println(Thread.currentThread().getName()+"="+i);
}
}
}
class Mythread9 implements Runnable{
@Override
public void run() {
for(int i=0;i<500;i++){
System.out.println(Thread.currentThread().getName()+"="+i);
}
}
}

ThreadTest 08
package 线程;
//线程让位
public class ThreadTest08 {
public static void main(String[] args) {
Thread T=new Thread(new MyThread6());
T.setName("名字");
T.start();
for (int i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()+"=="+i);
}
}
}
class MyThread6 implements Runnable{
@Override
public void run() {
for (int i=0;i<30;i++){
if(i%10==0){
Thread.yield();//让位
}
System.out.println(Thread.currentThread().getName()+"=="+i);
}
}
}


ThreadTest 09
package 线程;
//合并线程(不是合并在一起)
public class ThreadTest09 {
public static void main(String[] args) {
System.out.println("main begin");
Thread t=new Thread(new MyThread7());
t.setName("名字");
t.start();
try {
t.join();//当前线程进入阻塞,t线程执行,直到t线程结束,当前线程才可以继续
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("main over");
}
}
class MyThread7 implements Runnable{
@Override
public void run() {
for (int i=0;i<50;i++){
System.out.println(Thread.currentThread().getName()+"="+i);
}
}
} 
ThreadTest 10
package 线程.非线程安全;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
//第三种实现线程的方式:实现Callable接口
// 优点;可以获取到线程的执行结果
// 缺点:效率比较低,在获取t线程的执行结果时,当前线程受阻塞,效率较低
public class ThreadTest10 {
public static void main(String[] args) throws Exception{
//第一步:创建一个”未来任务类“对象,需要给一个Callable接口实现类对象
FutureTask task=new FutureTask(new Callable() {
@Override
public Object call() throws Exception {//call方法就相当于run方法,只不过这个有返回值
System.out.println("方法开始执行");
Thread.sleep(1000*5);
System.out.println("方法结束");
int a=10,b=20;
return a+b;//自动装箱(300结果变成Integer)
}
});
Thread t=new Thread(task);
t.start();
Object o=task.get();//获取t线程的返回结果(get方法的执行可能会导致当前线程阻塞)
System.out.println(o);
//main方法这里的程序需要执行必须等待get()方法的结束
//而get方法可能需要很久,因为get方法是为了拿另一个线程的结果
System.out.println("hello world");
}
}


线程安全
package 线程.线程安全;
public class Account {
private String actNo;
private double balance;
public Account() {
}
public Account(String actNo, double balance) {
this.actNo = actNo;
this.balance = balance;
}
public String getActNo() {
return actNo;
}
public void setActNo(String actNo) {
this.actNo = actNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public synchronized void withdraw(double money){//synchronized出现在实例方法上,一定锁的是this。不能是其他对象
//synchronized (this) {
double before = this.getBalance();
double after = before - money;
this.setBalance(after);
// }
}
}
package 线程.线程安全;
public class AccountThroad implements Runnable {
Account account;
public AccountThroad(Account account) {
this.account = account;
}
public AccountThroad() {
}
@Override
public void run() {
double money = 5000;
synchronized (account) {//如果用this,当前这个对象是AccountThroad,new了两次,两个内存地址,不是同一个对象,所以用account
account.withdraw(5000);
System.out.println(Thread.currentThread().getName() + "取款成功,余额:" + account.getBalance());
}
}
}package 线程.线程安全;
public class ThroadSafeTest {
public static void main(String[] args) {
Account a=new Account("001",10000);
Thread t1=new Thread(new AccountThroad(a));
Thread t2=new Thread(new AccountThroad(a));
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
非线程安全
package 线程.非线程安全;
public class Account {
private String actNo;
private double balance;
public Account() {
}
public Account(String actNo, double balance) {
this.actNo = actNo;
this.balance = balance;
}
public String getActNo() {
return actNo;
}
public void setActNo(String actNo) {
this.actNo = actNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void withdraw(double money){
double before=this.getBalance();
double after=before-money;
//t1执行到这里了,但是还没来得及执行下面的代码,t2线程进来了,此时一定会出问题,比如t1取了5000,还剩5000,下面还没执行,t2又取了5000,延迟了
this.setBalance(after);
}
}
package 线程.非线程安全;
public class AccountThroad implements Runnable {
Account account;
public AccountThroad(Account account) {
this.account = account;
}
public AccountThroad() {
}
@Override
public void run() {
double money=5000;
account.withdraw(5000);
System.out.println(Thread.currentThread().getName()+"取款成功,余额:"+account.getBalance());
}
}
package 线程.非线程安全;
public class ThroadSafeTest {
public static void main(String[] args) {
Account a=new Account("001",10000);
Thread t1=new Thread(new AccountThroad(a));
Thread t2=new Thread(new AccountThroad(a));
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
死锁
package 线程.死锁;
//线程t1锁住o1 然后睡了 同时 t2锁住o2 然后睡了 醒来t1就不能再锁住o2了
public class deadLock {
public static void main(String[] args) {
Object o1=new Object();
Object o2=new Object();
Thread t1=new MyThreat01(o1,o2);
Thread t2=new MyThreat02(o1,o2);
t1.start();
t2.start();
}
}
class MyThreat01 extends Thread{
Object o1;
Object o2;
public MyThreat01(Object o1,Object o2){
this.o1=o1;
this.o2=o2;
}
@Override
public void run() {
synchronized (o1){
try {
Thread.sleep(1000*1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (o2){
}
}
}
}
class MyThreat02 extends Thread{
Object o1;
Object o2;
public MyThreat02(Object o1,Object o2){
this.o1=o1;
this.o2=o2;
}
@Override
public void run() {
synchronized (o2){
try {
Thread.sleep(1000*1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (o1){
}
}
}
}
定时器
package 线程.定时器;
import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
//使用定时器指定定时任务
public class TimerTest {
public static void main(String[] args)throws Exception {
//创建定时器对象
Timer timer=new Timer();
//Timer timer=new Timer();//守护线程
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss ");
Date firstTime=sdf.parse("2022-09-20 13-12-00 ");
timer.schedule(new task(),firstTime,1000*2);
}
}
//编写一个定时任务类
class task extends TimerTask{
@Override
public void run() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss ");
String strTime=sdf.format(new Date());
System.out.println(strTime+":成功完成了一次数据备份");
}
}
守护线程
package 线程.守护线程;
public class ThreadTest10 {
public static void main(String[] args) {
Thread T=new BakDateThread();
T.setName("小孩");
//启动线程之前,将线程设置为守护线程
T.setDaemon(true);
T.start();
//主线程:是用户线程
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"=="+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
class BakDateThread extends Thread {
int i = 0;
@Override
public void run() {
while (true) {
//即使是死循环,但由于该线程是守护者,当用户线程结束,守护线程自动终止
System.out.println(Thread.currentThread().getName()+"--"+i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

版权声明:本文为m0_74195366原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。