/*
1,等待唤醒机制 交替输入两个人的姓名和性别,交替输出
class Res
{
private String name;
private String sex;
private boolean flag;
public synchronized void set(String name,String sex)
{
if (flag)
{
try
{
this.wait();
}
catch (Exception e)
{
}
}
this.name = name;
this.sex = sex;
flag = !flag;
this.notify();
}
public synchronized void get()
{
if (!flag)
{
try
{
wait();
}
catch (Exception e)
{
}
}
System.out.println(Thread.currentThread().getName()+name+sex);
flag = !flag;
notify();
}
}
class In implements Runnable
{
private Res r;
In(Res r)
{
this.r = r;
}
public void run()
{
int flag = 0;
while (true)
{
if (flag == 0)
{
r.set("张三","--男");
}
else
r.set("李四","女");
flag = ++flag%2;
}
}
}
class Out implements Runnable
{
private Res r;
Out(Res r)
{
this.r = r;
}
public void run()
{
while (true)
{
r.get();
}
}
}
class day12
{
public static void main(String[] args)
{
Res r = new Res();
new Thread(new In(r)).start();
new Thread(new In(r)).start();
new Thread(new Out(r)).start();
new Thread(new Out(r)).start();
}
}
7,使三个循环同时运行
class day12
{
public static void main(String[] args)
{
new Thread()
{
public void run()
{
for (int x = 0;x < 100 ;x++ )
{
System.out.print(Thread.currentThread().getName()+x);
}
}
}.start();
Runnable r = new Runnable()
{
public void run()
{
for (int x = 0;x < 100 ;x++ )
{
System.out.print(Thread.currentThread().getName()+x);
}
}
};
new Thread(r).start();
for (int x = 0;x< 100; x++)
{
System.out.print(Thread.currentThread().getName()+x);
}
}
}
3,停止线程
4,守护线程
5,线程join方法
6,线程yield方法
class MThread implements Runnable
{
private boolean flag = true;
private int x = 0;
public void changeFlag()
{
flag = false;
}
public synchronized void run()
{
while (flag)
{
System.out.println(Thread.currentThread().getName()+(++x));
Thread.yield();
}
}
}
class day12
{
public static void main(String[] args) throws Exception
{
// new Thread(new MThread()).start();
Thread t = new Thread(new MThread());
t.setDaemon(true);//守护线程,随主线程停止
MThread stop = new MThread();
new Thread(stop).start();
for (int x = 0;x < 100 ;x ++ )
{
if (x == 50)
{
stop.changeFlag();
}
System.out.print(x);
}
// Thread t2 = new Thread(new MThread());
// t2.join();
// t2.start();
}
}
2,生产者消费者,两种锁
*/
import java.util.concurrent.locks.*;
class Goods
{
private String name;
private int count;
private boolean flag;
private int x;
private Lock lock = new ReentrantLock();
private Condition pro = lock.newCondition();
private Condition con = lock.newCondition();
public void in(String name)
{
lock.lock();
try
{
while (flag)
{
pro.await();
}
this.name = name;
System.out.println(Thread.currentThread()+name+(++count));
flag = true;
con.signal();
}
catch (InterruptedException ie)
{
}
finally
{
lock.unlock();
}
}
public void out()
{
lock.lock();
try
{
while (!flag)
{
con.await();
}
System.out.println(Thread.currentThread()+name+"消费");
flag = false;
pro.signal();
}
catch (InterruptedException ie)
{
}
finally
{
lock.unlock();
}
}
}
class Pro implements Runnable
{
private Goods g;
Pro (Goods g)
{
this.g = g;
}
public void run()
{
while (true)
{
g.in("汽车");
}
}
}
class Cons implements Runnable
{
private Goods g;
Cons(Goods g)
{
this.g = g;
}
public void run()
{
while (true)
{
g.out();
}
}
}
class day12
{
public static void main(String[] args)
{
Goods g = new Goods();
new Thread(new Pro(g)).start();
new Thread(new Pro(g)).start();
new Thread(new Cons(g)).start();
new Thread(new Cons(g)).start();
}
}