方法一 通过访问共享变量的方式(注:需要处理同步问题) 
方法二 通过管道流

其中方法一有两种实现方法,即 
方法一a)通过内部类实现线程的共享变量 

public class Innersharethread {
public static void main(String[] args) {
Mythread mythread = new Mythread();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
}
}
class Mythread {
int index = 0; private class InnerThread extends Thread {
public synchronized void run() {
while (true) {
System.out.println(Thread.currentThread().getName()
+ "is running and index is " + index++);
}
}
} public Thread getThread() {
return new InnerThread();
}
} /**
* 通过内部类实现线程的共享变量
*
*/
public class Innersharethread {
public static void main(String[] args) {
Mythread mythread = new Mythread();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
}
}
class Mythread {
int index = 0; private class InnerThread extends Thread {
public synchronized void run() {
while (true) {
System.out.println(Thread.currentThread().getName()
+ "is running and index is " + index++);
}
}
} public Thread getThread() {
return new InnerThread();
}
}

b)通过实现Runnable接口实现线程的共享变量 

public class Interfacaesharethread {
public static void main(String[] args) {
Mythread mythread = new Mythread();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
}
} /* 实现Runnable接口 */
class Mythread implements Runnable {
int index = 0; public synchronized void run() {
while (true)
System.out.println(Thread.currentThread().getName() + "is running and
the index is " + index++);
}
} /**
* 通过实现Runnable接口实现线程的共享变量 */
public class Interfacaesharethread {
public static void main(String[] args) {
Mythread mythread = new Mythread();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
}
} /* 实现Runnable接口 */
class Mythread implements Runnable {
int index = 0; public synchronized void run() {
while (true)
System.out.println(Thread.currentThread().getName() + "is running and
the index is " + index++);
}
}

方法二(通过管道流): 

public class CommunicateWhitPiping {
public static void main(String[] args) {
/**
* 创建管道输出流
*/
PipedOutputStream pos = new PipedOutputStream();
/**
* 创建管道输入流
*/
PipedInputStream pis = new PipedInputStream();
try {
/**
* 将管道输入流与输出流连接 此过程也可通过重载的构造函数来实现
*/
pos.connect(pis);
} catch (IOException e) {
e.printStackTrace();
}
/**
* 创建生产者线程
*/
Producer p = new Producer(pos);
/**
* 创建消费者线程
*/
Consumer c = new Consumer(pis);
/**
* 启动线程
*/
p.start();
c.start();
}
} /**
* 生产者线程(与一个管道输入流相关联)
*
*/
class Producer extends Thread {
private PipedOutputStream pos; public Producer(PipedOutputStream pos) {
this.pos = pos;
} public void run() {
int i = 8;
try {
pos.write(i);
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* 消费者线程(与一个管道输入流相关联)
*
*/
class Consumer extends Thread {
private PipedInputStream pis; public Consumer(PipedInputStream pis) {
this.pis = pis;
} public void run() {
try {
System.out.println(pis.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}

最新文章

  1. Android开发常用属性
  2. UML:包图
  3. 第一课 Hello
  4. noip2014普及组 比例简化
  5. 黄聪:PHP json_encode中文乱码解决方法
  6. asp.net webuploader粘贴,拖拽,点击上传图片
  7. L - Oil Deposits
  8. java web 学习(1)
  9. JavaScript --------------继前面继承方式-------总结
  10. 【转】《分享一下我研究SQLSERVER以来收集的笔记》未整理
  11. 一步一步深入spring(7)-- 整合spring和JDBC的环境
  12. 洛谷 [P2024] 食物链
  13. Delphi XE2 新增 System.Zip 单元, 可用一句话压缩整个文件夹了
  14. Centos系统下 Gitolite安装与相关配置(git权限控制软件)
  15. [maven] 项目不同环境自动打包
  16. windos或linux中 which命令 查看当前要执行的命令所在的路径
  17. 在eclipse中修改生成的注释类、方法的作者、时间
  18. 洛谷 P3382 【模板】三分法
  19. Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块
  20. java代码---------比较随机数的大小---我搞不懂啊

热门文章

  1. Html+CSS命名规范:
  2. EasyUI中combotree允许多选的时候onSelect事件会重复触发onCheck事件
  3. 常用的WebService调用接口
  4. Excel对话框大全
  5. MongoDB笔记(三)启动命令mongod的参数
  6. 【转】PHP程序员的技术成长规划
  7. jquery 实现复选框单选
  8. 【转】PL/SQL Developer使用技巧、快捷键
  9. HTML5 改良的 input 元素的种类
  10. (转载)delphi 把图片存入数据库