有一段时间没有更新博客
因为也在学习过程中有些阻碍,难以跟进,过段时间再去写之前学习的,会发现很多问题,还是要多练习,温故而知新
多线程练习一
设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,不要求轮流
(其实是个很简单的多线程设计,可是本博开始做的时候脑子卡壳)
定义一个类,封装加减这两个方法
class Count{
int j;
public void add(){
while (true) { //无限循环
synchronized (this) { //同步
j++;
System.out.println(Thread.currentThread().getName()+"---"+j);
} <span style="font-family: Arial, Helvetica, sans-serif;"> //为方便看出是哪条线程拿的所以打印:</span><span style="font-family: Arial, Helvetica, sans-serif;">Thread.currentThread().getName()</span>
}
}
public void jian(){
while (true) {
synchronized (this) {
j--;
System.out.println(Thread.currentThread().getName()+"---"+j);
}
}
}
}
另外定义加减这两个类分别调用加减两个方法
class Add implements Runnable{ //接Runnable接口
private Count co;
public Add(Count co){ //确定数据共享来源相同
super();
this.co = co;
}
@Override
public void run() {
// TODO Auto-generated method stub
co.add(); //调用Count里的方法
}
}
class Sub implements Runnable{
private Count co;
public Sub(Count co){ <span style="font-family: Arial, Helvetica, sans-serif;">//确定数据共享来源相同</span>
super();
this.co = co;
}
@Override
public void run() {
// TODO Auto-generated method stub
co.jian();
}
}
创建线程
public class Thread1 {
public static void main(String[] args) {
Count co = new Count();
Add add = new Add(co); //对象传参
Sub sub = new Sub(co);
new Thread(add).start(); //加线程
new Thread(add).start();
new Thread(sub).start(); //减线程
new Thread(sub).start();
}
}
运行结果(随机)
多线程练习二
子线程循环10次,接着主线程循环100次,接着子线程又循环10次,再回带主线程 来回循环50次
方法一
class Cycle implements Runnable{
private boolean boat = true;
@Override
public void run() { //子
synchronized(this){
while (!boat) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 10; i++) { //循环
System.out.println(Thread.currentThread().getName()+"子线程运行:"+i);
}
boat = false;
this.notify();
}
}
public void main(){ //主
synchronized(this){
while (boat) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i <100; i++) {
System.out.println(Thread.currentThread().getName()+"主线程运行:"+i);
}
boat = true;
this.notify();
}
}
}
创建线程
public class Thread2 {
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
Cycle cycle = new Cycle();
new Thread(cycle).start(); //子线程
cycle.main(); //主线程
System.out.println("**********");
}
}
}
方法二(锁)
class Cycle2 implements Runnable{
Lock lock = new ReentrantLock();
Condition con = lock.newCondition();
boolean able = true;
@Override
public void run() {
lock.lock();
while (!able) {
try {
con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"子线程出发:"+i);
}
able = false;
con.signalAll();
} finally {
lock.unlock();
}
}
public void main(){
lock.lock();
while (able) {
try {
con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"主线程出发:"+i);
}
able = true;
con.signalAll();
} finally {
lock.unlock();
}
}
}
创建线程
public class Thread2 {
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
Cycle2 cycle2 = new Cycle2();
new Thread(cycle2).start();
cycle2.main();
System.out.println("###########");
}
}
}
运行结果