继承Thread类方式创建多线程
class ThreadDemo {
public static void main(String[] args) {
Demo d = new Demo("线程1");
Demo2 d1 = new Demo2("线程2");
d1.start();
d.start();
for(int x = 0; x <30; x++)
System.out.println("hello world--"+x);
}
}
class Demo extends Thread {
Demo (String name) {
super (name);
}
public void run () {
for(int x = 0; x <30; x++ )
System.out.println(this.getName()+"--Demo run--"+x);
}
}
class Demo2 extends Thread {
Demo2 (String name) {
super (name);
}
public void run () {
for(int x = 0; x <30; x++)
System.out.println(this.getName()+"--Demo2 run--"+x);
}
}
实现Runnable接口方式创建多线程
class ThreadDemo2 {
public static void main(String[] args) {
Demo d = new Demo ();
Thread d1 = new Thread (d); //将Runnable的子类传递给Thread类的构造函数
Thread d2 = new Thread (d);
Thread d3 = new Thread (d);
Thread d4 = new Thread (d);
d1.start(); //开启线程 调用run方法
d2.start();
d3.start();
d4.start();
}
}
class Demo implements Runnable { //实现Runnable接口
private int tick = 100;
Object o = new Object();
public void run () { //覆盖run方法
while (true) {
synchronized (o) { //线程安全性 同步代码块
if (tick>0) {
try {
Thread.sleep(10);
}
catch(Exception e) {
e.toString();
}
System.out.println(Thread.currentThread().getName()+"...卖..."+tick--);
}
}
}
}
}
同步函数
class ThreadDemo3 {
public static void main(String[] args) {
Demo d = new Demo ();
Thread d1 = new Thread (d); //将Runnable的子类传递给Thread类的构造函数
Thread d2 = new Thread (d);
Thread d3 = new Thread (d);
Thread d4 = new Thread (d);
d1.start(); //开启线程 调用run方法
d2.start();
d3.start();
d4.start();
}
}
class Demo implements Runnable { //实现Runnable接口
private int tick = 1000;
Object o = new Object();
public void run () { //覆盖run方法
while (true) {
show();
}
}
public synchronized void show () { //同步函数
if (tick>0) {
try {
Thread.sleep(10);
}
catch(Exception e) {
e.toString();
}
System.out.println(Thread.currentThread().getName()+"...卖..."+tick--);
}
}
}
class BankDemo {
public static void main(String[] args) {
Person p = new Person();
Thread p1 = new Thread(p);
Thread p2 = new Thread(p);
p1.start();
p2.start();
}
}
class Person implements Runnable {
private Bank b = new Bank();
public void run() {
for (int x =0 ; x<3; x++) {
b.add(100);
}
}
}
class Bank {
private int sum;
public synchronized void add(int n) { //同步函数
sum = sum + n;
try {
Thread.sleep(10);
}
catch(Exception e){}
System.out.println("sum="+sum);
}
}
同步代码块
class DeathLock {
public static void main(String[] args) {
Demo d = new Demo ();
Thread d1 = new Thread (d); //将Runnable的子类传递给Thread类的构造函数
Thread d2 = new Thread (d);
Thread d3 = new Thread (d);
Thread d4 = new Thread (d);
d1.start(); //开启线程 调用run方法
d2.start();
d3.start();
d4.start();
}
}
class Demo implements Runnable { //实现Runnable接口
private int tick = 100;
Object o = new Object();
boolean flag = true;
public void run () { //覆盖run方法
if(flag) {
while (true) {
synchronized (this) { //线程安全性 同步代码块
if (tick>0) {
try {
Thread.sleep(10);
}
catch(Exception e) {
e.toString();
}
System.out.println(Thread.currentThread().getName()+"...卖..."+tick--);
}
}
}
}
else
while(true)
show();
}
public synchronized void show() {
flag = false;
System.out.println(Thread.currentThread().getName()+"...卖..."+tick--);
}
}
懒汉式线程不安全解决办法(不完全)
class SingleDemo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
class Single { //懒汉式
private static Single s = null;
private Single () {};
public static Single getInstance() {
if(s==null) { //双重判断提高效率
synchronized(Single.class) {
if(s==null)
s = new Single();
}
}
return s;
}
}
死锁
class DeahLockDemo {
public static void main(String[] args) {
Thread t1 = new Thread (new Test(true));
Thread t2 = new Thread (new Test(false));
t1.start();
t2.start();
}
}
class Test implements Runnable {
private boolean flag;
Test (boolean flag) {
this.flag = flag;
}
public void run() {
if (flag) {
synchronized(Mylock.locka) {
System.out.println("ifaaaaaaaa");
synchronized(Mylock.lockb) {
System.out.println("ifbbbbbbbbb");
}
}
}
else {
synchronized(Mylock.lockb) {
System.out.println("elsebbbbbbbbbb");
synchronized(Mylock.locka) {
System.out.println("elseaaaaaaaaa");
}
}
}
}
}
class Mylock {
static Object locka = new Object();
static Object lockb = new Object();
}
线程间的通信
class Res {
String sex;
String name;
}
class Input implements Runnable {
private Res r;
Input(Res r) {
this.r = r;
}
public void run () {
int x = 0;
while(true) {
synchronized (Res.class) {
if(x==0) {
r.name = "mike";
r.sex = "man";
}
else {
r.name = "丽丽";
r.sex = "女";
}
x = (x+1)%2;
}
}
}
}
class Output implements Runnable {
Res r;
Output(Res r) {
this.r = r;
}
public void run() {
while(true) {
synchronized(Res.class) {
System.out.println(r.name+"...."+r.sex);
}
}
}
}
class InputOutputDemo {
public static void main(String[] args) {
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread (in);
Thread t2 = new Thread (out);
t1.start();
t2.start();
}
}