启动线程的第一种方法
package com.bjpowernode.java.thread;
public class ThreadTest02 {
public static void main(String[] args) {
MyThread myThread=new MyThread();
myThread.start();
for (int i=0;i<1000;i++){
System.out.println("主线程---->"+i);
}
}
}
class MyThread extends Thread {
@Override
public void run() {
for (int i=0;i<1000;i++){
System.out.println("分支线程启动--->"+i);
}
}
}
启动线程的第二种方式
package com.bjpowernode.java.thread;
public class ThreadTest03 {
public static void main(String[] args) {
MyRunnable myRunnable=new MyRunnable();
Thread thread=new Thread(myRunnable);
thread.start();
for (int i=0;i<1000;i++){
System.out.println("主线程---->"+i);
}
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i=0;i<1000;i++){
System.out.println("分支线程---->"+i);
}
}
}
采用匿名内部类去启动线程(使用接口去启动线程)
package com.bjpowernode.java.thread;
public class ThreadTest04 {
public static void main(String[] args) {
Thread t=new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<100;i++){
System.out.println("分支线程的启动"+i);
}
}
});
t.start();
for (int i=0;i<100;i++){
System.out.println("主线程启动"+i);
}
}
}
获取当前线程
package com.bjpowernode.java.thread;
public class ThreadTest05 {
public static void main(String[] args) {
Thread currentThread =Thread.currentThread();
System.out.println(currentThread.getName());
MyThread2 myThread2=new MyThread2();
myThread2.setName("ttt");
String s=myThread2.getName();
System.out.println(s);
myThread2.start();
}
}
class MyThread2 extends Thread {
@Override
public void run() {
for (int i=0;i<100;i++){
System.out.println("分支线程"+i);
}
}
}
让当前线程处于阻塞状态(sleep方法)
package com.bjpowernode.java.thread;
public class ThreadTest06 {
public static void main(String[] args) {
for (int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"---->"+i);
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
阻塞面试题
package com.bjpowernode.java.thread;
public class ThreadTest07 {
public static void main(String[] args) {
Thread t=new MyThread3();
t.setName("tt");
System.out.println(t.getName());
t.start();
try{
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hello world");
}
}
class MyThread3 extends Thread {
public void run(){
for (int i=0;i<1000;i++){
System.out.println(Thread.currentThread().getName()+"----->"+i);
}
}
}
怎么叫醒一个正在睡眠的线程
package com.bjpowernode.java.thread;
public class ThreadTest08 {
public static void main(String[] args) {
Thread t=new Thread(new MyRunnable2());
t.setName("t");
t.start();
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
}
class MyRunnable2 implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"---->begin");
try {
Thread.sleep(1000*60*60*24*365);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"---->end");
}
}
如何强行终止一个线程
package com.bjpowernode.java.thread;
public class ThreadTest09 {
public static void main(String[] args) {
Thread t=new Thread(new MyRunnable3());
t.setName("t");
t.start();
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.stop();
}
}
class MyRunnable3 implements Runnable {
@Override
public void run() {
for (int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"---->"+i);
try {
Thread.sleep(1000*1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
合理终止一个线程
package com.bjpowernode.java.thread;
public class ThreadTest10 {
public static void main(String[] args) {
MyRunnable4 r=new MyRunnable4();
Thread t=new Thread(r);
t.setName("t");
t.start();
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.printStackTrace();
}
r.flag=false;
}
}
class MyRunnable4 implements Runnable {
boolean flag=true;
@Override
public void run() {
for (int i=0;i<10;i++){
if (flag){
System.out.println(Thread.currentThread().getName()+"---->"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
return;
}
}
}
}
线程排队执行
package com.bjpowernode.java.threadsafe2;
public class Account {
private String actno;
private double balance;
public Account(String actno, double balance) {
this.actno = actno;
this.balance = balance;
}
public Account() {
}
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){
synchronized (this){
double before=this.getBalance();
double after=before-money;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setBalance(after);
}
}
}
package com.bjpowernode.java.threadsafe2;
public class AccountThread extends Thread {
private Account act;
public AccountThread(Account act) {
this.act = act;
}
@Override
public void run() {
double money=5000;
act.withdraw(money);
System.out.println("账户"+act.getActno()+"取款成功,余额"+act.getBalance());
}
}
package com.bjpowernode.java.threadsafe2;
public class Test {
public static void main(String[] args) {
Account act=new Account("act-001",10000);
Thread t1=new AccountThread(act);
Thread t2=new AccountThread(act);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
死锁(必须会)
package com.bjpowernode.java.deadlock;
public class DeadLock {
public static void main(String[] args) {
Object o1=new Object();
Object o2=new Object();
Thread t1=new MyThread1(o1,o2);
Thread t2=new MyThread2(o1,o2);
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
Object o1;
Object o2;
public MyThread1(Object o1, Object o2) {
this.o1 = o1;
this.o2 = o2;
}
public void run(){
synchronized (o1){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2){
}
}
}
}
class MyThread2 extends Thread {
Object o1;
Object o2;
public MyThread2(Object o1, Object o2) {
this.o1 = o1;
this.o2 = o2;
}
public void run(){
synchronized (o2){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1){
}
}
}
}
守护线程
package com.bjpowernode.java.thread;
public class ThreadTest14 {
public static void main(String[] args) {
Thread t=new BakDataThread();
t.setName("t");
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) {
e.printStackTrace();
}
}
}
}
class BakDataThread extends Thread {
public void run(){
int i=0;
while(true){
System.out.println(Thread.currentThread().getName()+"----->"+(++i));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
定时器(以后在SpringTask中可以用)
- SpringTask中也是基java.util.Timer下去编写的
package com.bjpowernode.java.thread;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTest {
public static void main(String[] args) throws Exception {
Timer timer=new Timer();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date firstTime=sdf.parse("2022-04-05 17:14:30");
timer.schedule(new LogTimerTask(),firstTime,1000*10);
}
}
class LogTimerTask 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+"完成了一次数据备份");
}
}
实现线程的第三种方式(Callable接口)
package com.bjpowernode.java.thread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadTest15 {
public static void main(String[] args) {
FutureTask task=new FutureTask(new Callable() {
@Override
public Object call() throws Exception {
System.out.println("call begin");
Thread.sleep(1000*10);
System.out.println("call end");
int a=100;
int b=200;
return a+b;
}
});
Thread t=new Thread(task);
t.start();
try {
Object obj=task.get();
System.out.println("线程执行结果:"+obj);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("hello");
}
}
生产和消费平衡
package com.bjpowernode.java.thread;
import java.util.ArrayList;
import java.util.List;
public class ThreadTest16 {
public static void main(String[] args) {
List list=new ArrayList();
Thread t1=new Thread(new Producer(list));
Thread t2=new Thread(new Consumer(list));
t1.setName("生产者线程");
t2.setName("消费者线程");
t1.start();
t2.start();
}
}
class Producer implements Runnable {
private List list;
public Producer(List list) {
this.list = list;
}
public Producer() {
}
@Override
public void run() {
while (true){
synchronized (list){
if(list.size()>0){
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object obj=new Object();
list.add(obj);
System.out.println(Thread.currentThread().getName()+"----->"+obj);
list.notify();
}
}
}
}
class Consumer implements Runnable {
private List list;
public Consumer(List list) {
this.list = list;
}
public Consumer() {
}
@Override
public void run() {
while (true){
synchronized (list){
if (list.size()==0){
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object obj=list.remove(0);
System.out.println(Thread.currentThread().getName()+"---->"+obj);
list.notify();
}
}
}
}
|