1. 实现有两种
继承thread
实现runnable 接口
例子: sleep setpriority
public class XianCheng1 extends Thread {
@Override
public void run() {
show();
}
public void show(){
for(int i =0;i<100;i++){
System.out.println("!!!!!!!!!!"+i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
XianCheng1 xiancheng = new XianCheng1();
try {
xiancheng.sleep(1000); //睡眠
xiancheng.setPriority(2);//设置优先级
xiancheng.start();//运行
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i =0;i<1000;i++){
System.out.println("我是主-------------"+i);
}
}
join yield 使用
join 合并线程 必须把那个线程运行完
yield 让出 cpu
例子:
join
public static void main(String[] args) {
// TODO Auto-generated method stub
XianCheng4 xiancheng = new XianCheng4();
xiancheng.start();
try {
xiancheng.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<1000;i++){
System.out.println("sssdddd============");
}
}
public void run() {
for(int i=0;i<1000;i++){
System.out.println("aaaaa"+i);
}
}
yield 使用
例子: 一到10整除就让
public static void main(String[] args) {
// TODO Auto-generated method stub
XianCheng5 xiancheng = new XianCheng5();
xiancheng.start();
for(int i=0;i<1000;i++){
System.out.println("ssssss========="+i);
}
}
@Override
public void run() {
for(int i =0;i<1000;i++){
System.out.println("aaaaaaaaaaaaa"+i);
if(i%10==0){
yield();
}
}
}
线程同步: 重点
使用锁
例子:
使用关键字: synchronized
public static void main(String[] args) {
// TODO Auto-generated method stub
Test1 test = new Test1();
XianCheng6 xiancheng = new XianCheng6(test);
XianCheng6 xiancheng1 = new XianCheng6(test);
xiancheng.start();
xiancheng1.start();
System.out.println("rttt");
}
----
public class XianCheng6 extends Thread{
public Test1 test;
public XianCheng6(Test1 test) {
this.test =test;
}
public void run(){
test.show();
}
}
------
public class Test1 {
public static int a ;
public synchronized void show(){
a++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("_____"+a);
}
}
编写的时候注意死锁
死锁都不会动了
一个死锁的例子:
public static void main(String[]args){
XianCheng7 xianCheng7 = new XianCheng7();
xianCheng7.i =0;
xianCheng7.start();
XianCheng7 xianCheng2 = new XianCheng7();
xianCheng2.i =1;
xianCheng2.start();
}
---
public class XianCheng7 extends Thread{
static Object a= new Object();
static Object b = new Object();
int i;
@Override
public void run() {
// TODO Auto-generated method stub
if(i==0){
synchronized(a){
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(b){
System.out.println("gggggggg");
}
}
}
if(i==1){
synchronized(b){
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(a){
System.out.println("-----------------");
}
}
}
}
}
线程的一个例子 面试出来的
public class XianCheng8 extends Thread {
int chengji =100;
public synchronized void show() throws InterruptedException{
chengji =2000;
Thread.sleep(5000);
System.out.println("chengji"+chengji);
}
public void m2() throws InterruptedException{
Thread.sleep(2500);
System.out.println("chengji ---"+chengji);
}
public void run(){
try {
show();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[]args) throws InterruptedException{
XianCheng8 xiancheng = new XianCheng8();
xiancheng.start();
xiancheng.m2();
}
}
成绩是2000;
m2 方法加上 synchronized
就是100;
生产消费问题
--- 产品类
public class ManTou {
int id ;
public ManTou(int id){
this.id =id;
---定义一个放产品的地
public class kuang {
int index=0;
ManTou[]mantous = new ManTou[6];
//放馒头
public synchronized void push(ManTou mantou){
if(index == mantous.length) {
try {
this.wait();//让当前线程等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
mantous[1]=mantou;
index++;
}
//拿出馒头
public synchronized ManTou pop(){
if(index ==0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return mantous[index];
}
}
}
}
--- 生产产品类
public class Por extends Thread {
public kuang kuang;
public Por(kuang kuang){
this.kuang =kuang;
}
@Override
public void run() {
for(int i=0;i<20;i++){
ManTou mantou = new ManTou(i);
kuang.push(mantou);
System.out.println("生产了:" + i);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
------消费产品类
public class Pup extends Thread{
public kuang kuang ;
public Pup(kuang kuang){
this.kuang =kuang;
}
@Override
public void run() {
for(int i=0;i<20;i++){
ManTou mantou = new ManTou(i);
kuang.pop();
System.out.println("消费了: " + i);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
-----测试
public static void main(String[] args) {
// TODO Auto-generated method stu
kuang kuang = new kuang();
Por por = new Por(kuang);
Pup pup = new Pup(kuang);
por.start();
pup.start();
}