//测试类
package test20220512;
/*
* 实现Runnable接口实现多线程,适合处理共享数据
*/
public class myThreadJiekou implements Runnable {
private int bao=10;
public void run() {
for(int i=0;i<10;i++) {
if(bao>0)
System.out.println(Thread.currentThread().getName()+"捡了第"+bao--+"宝贝");
}
try{
Thread.sleep(1000);
}catch (Exception e) {
System.out.println();
}
}
}
//调用类
package test20220512;
import java.io.InterruptedIOException;
public class ceshijiekou {
public static void main(String[] args) {
myThreadJiekou p=new myThreadJiekou();
Thread t=new Thread(p,"张");
t.setPriority(Thread.NORM_PRIORITY);//设置优先级
Thread t1=new Thread(p,"黎");
t1.setPriority(Thread.NORM_PRIORITY+1);
Thread t2=new Thread(p,"刘");
t2.setPriority(Thread.NORM_PRIORITY+2);
Thread t3=new Thread(p,"郭");
t3.setPriority(Thread.NORM_PRIORITY-1);
t.start();
t1.start();
t2.start();
t3.start();
System.out.println(Thread.currentThread().getName()+"执行");
try { //加入main县城,等子线程执行完,在执行主线程
t.join();
t1.join();
t2.join();
t3.join();
}catch(InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("结束");
}
}
}
主线程--》子线程传递信息,把要传的信息作为线程的属性
package test20220518;
import java.util.Scanner;
/*
* 主线程--》子线程传递信息,把要传的信息作为线程的属性
* 子线程--》主线程传递信息,把要传的信息作为线程的属性
*/
public class myrunnable implements Runnable {
private int n;
private int sum;
private String name;
public myrunnable(int n) {
this.n=n;
}
public myrunnable() {
n=100;
}
public void set_n(int n) {
this.n=n;
}
public int get_sum(int sum) {
return sum;
}
public void run() {//求0~n内的偶数和
for(int i=0;i<=n;i+=2) {
sum+=i;
}
System.out.println(Thread.currentThread().getName()+":"+sum);
}
}
package test20220518;
public class myrunnables {
public static void main(String[] args) {
myrunnable m=new myrunnable();//主线程把信息传给子线程
Thread t=new Thread(m,"1");
t.start();
//延时
try {
Thread.sleep(1000);
}catch (Exception e) {
System.out.println("出错");
}
//在执行
Thread t1=new Thread(m,"2");
t1.start();
//延时
try {
Thread.sleep(1000);
}catch (Exception e) {
System.out.println("出错了");
}
//在执行
myrunnable m2=new myrunnable();//主线程把信息传给子线程
Thread t2=new Thread(m2,"3");
m2.set_n(1000000);//调用
t2.start();
try {
t.join();
t1.join();
t2.join();
}catch (Exception e) {
// TODO: handle exception
}
System.out.println(Thread.currentThread().getName()+":"+m2.get_sum(1000));
}
}
、、、、、、、、第三种、、、、、、、、、、、、、、、
package enterprise;
public class bank {
private int money=0;
/*
* synchronized:互斥操作(一个进完成了,才能进行下一个进程)
*/
public void save(int money) { //方法头部加上synchronized
int temp=this.money;
temp+=money;
this.money=temp;
System.out.println(Thread.currentThread().getName()+":向卡存钱"+money+",卡内金额:"+this.money);
}
public void save1(int money) { //方法头部加上synchronized
int temp=this.money;
temp-=money;
this.money=temp;
System.out.println(Thread.currentThread().getName()+":向卡拿钱"+money+",卡内金额:"+this.money);
}
}
package enterprise;
public class cunqian implements Runnable {
private bank bk;
public cunqian(bank bk)
{
this.bk=bk;
}
public void run() {
//synchronized (this) {
for(int i=0;i<1;i++) {
bk.save(200);
bk.save(300);
bk.save1(150);
}
//}
}
}
package enterprise;
public class test {
public static void main(String[] args) {
bank bk=new bank();
cunqian s=new cunqian(bk);
Thread m=new Thread(s,"爸爸");
m.start();
bank bk1=new bank();
cunqian s1=new cunqian(bk1);
Thread m1=new Thread(s1,"妈妈");
m1.start();
bank bk2=new bank();
cunqian s2=new cunqian(bk2);
Thread m2=new Thread(s2,"姐姐");
m2.start();
try {
m.join();
m1.join();
m2.join();
}catch (Exception e) {
}
}
}