简单例子:
package 多线程;
public class Thread1 implements Runnable{
@Override
public void run() {
// TODO 自动生成的方法存根
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
package 多线程;
public class Test1 {
public static void main(String[] args) {
Thread1 thread1 = new Thread1();
Thread1 thread2 = new Thread1();
Thread thread = new Thread(thread1);
Thread threads = new Thread(thread2);
thread.start();
threads.start();
}
}
重写star
package 多线程;
public class ThreadTest implements Runnable{
private Thread thread;
private String threadName;
public void run() {
// TODO 自动生成的方法存根
for(int i = 0;i<1000;i++){
System.out.println(threadName+" "+i);
try {
thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public ThreadTest(String threadName) {
super();
this.threadName = threadName;
}
public void setThreadName(String threadName) {
this.threadName = threadName;
}
public void start() {
// TODO 自动生成的方法存根
if(thread == null){
thread= new Thread(this,threadName);
thread.start();
}
}
}
package 多线程;
public class Test {
public static void main(String[] args) {
ThreadTest thread = new ThreadTest("强月城");
thread.start();
ThreadTest thread1 = new ThreadTest("优快云");
thread1.start();
}
}
优快云 0
强月城 0
强月城 1
优快云 1
强月城 2
优快云 2
强月城 3
优快云 3
强月城 4
优快云 4。。。。。
join 等待完成
package 多线程join;
public class Thread_join implements Runnable {
@Override
public void run() {
// TODO 自动生成的方法存根
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
}
package 多线程join;
public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread_join thread_join1 = new Thread_join();
Thread thread1 = new Thread(thread_join1);
thread1.start();
for(int i = 0;i<10;i++){
System.out.println("main "+i);
if(i == 5){
thread1.join();
}
}
}
}
wait 等待 notifyAll 唤醒 synchronized同步
package 多线程wait_notifyAll_synchronized;
public class Account {
//编号,余额
private String accountNo;
private double balance;
private boolean flag = false;
public String getAccountNo() {
return accountNo;
}
public double getBalance() {
return balance;
}
public Account(String accountNo, double balance) {
this.accountNo = accountNo;
this.balance = balance;
}
public synchronized void draw(double drawAmount) {
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}else {
if(balance-drawAmount>=0){
System.out.println(Thread.currentThread().getName()+"取钱"+drawAmount);
balance = balance-drawAmount;
System.out.println("余额为:"+balance);
}
flag = false;
notifyAll();
}
}
public synchronized void deposit(double depositAmount) {
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}else {
System.out.println(Thread.currentThread().getName()+"存钱"+depositAmount);
balance = balance+depositAmount;
System.out.println("余额为:"+balance);
flag = true;
notifyAll();
}
}
}
package 多线程wait_notifyAll_synchronized;
public class DrawThread implements Runnable{
private Account account ;
private double drawAmount;
public DrawThread(Account account, double drawAmount) {
super();
this.account = account;
this.drawAmount = drawAmount;
}
@Override
public void run() {
// TODO 自动生成的方法存根
for(int i =0;i<10;i++){
account.draw(drawAmount);
}
}
}
package 多线程wait_notifyAll_synchronized;
public class DepositThread implements Runnable{
private Account account ;
private double depositAccount;
public DepositThread(Account account, double depositAccount) {
super();
this.account = account;
this.depositAccount = depositAccount;
}
@Override
public void run() {
// TODO 自动生成的方法存根
for(int i =0;i<10;i++){
account.deposit(depositAccount);
}
}
}
package 多线程wait_notifyAll_synchronized;
public class Demo {
public static void main(String[] args) {
Account account = new Account("qyc", 0);
DrawThread drawThread = new DrawThread(account, 700);
DepositThread depositThread = new DepositThread(account, 600);
Thread thread = new Thread(drawThread);
Thread thread2 = new Thread(depositThread);
thread.start();
thread2.start();
}
}
Thread-1存钱600.0
余额为:600.0
Thread-1存钱600.0
余额为:1200.0
Thread-0取钱700.0
余额为:500.0
Thread-1存钱600.0
余额为:1100.0
Thread-0取钱700.0
余额为:400.0
Thread-1存钱600.0
余额为:1000.0
Thread-0取钱700.0
余额为:300.0
Thread-1存钱600.0
余额为:900.0
Thread-0取钱700.0
余额为:200.0