导航:首页 > 编程语言 > java多线程源码

java多线程源码

发布时间:2022-05-07 07:28:18

1. 什么是java多线程编程

一、 什么是多线程:

我们现在所使用操作系统都是多任务操作系统(早期使用的DOS操作系统为单任务操作系统),多任务操作指在同一时刻可以同时做多件事(可以同时执行多个程序)。

2. java哪个框架的多线程源码值得学习

最值得学的当属Spring框架了。不过学之前还是先熟悉它里面的各种概念好一些。 如果想零碎点学的话,Apache网站上的一些java工具,比如ant之类的,可以在了解其作用的情况下看源码分析功能的实现。

3. 求JAVA多线程编程代码

测试过了,没问题。基本思路,实例化一个桥类,谁得到桥的可用标志谁过桥。
我第一个看到这个100分的,说实话,知道你是个学生要代码而已,线程类好久没练手了,练习一下而已,否则真不会给你写代码。因为我读书的时候也发过类似的求助,知道什么感受。不懂的时候真的没办法,所以告诉你思路。
package cn.thread;

public class Through_out_bridge {

public static void main(String[] args) {
Bridge b = Bridge.getInstance();//实例化桥
//实例化左端9个人,此时所有人都不能过桥,桥的可以状态标志为不可以用
for (int i = 1; i <= 9; i++) {
Thread t = new Thread(new Person(false, i, b));
t.start();
}
//实例化右端12个人,此时所有人都不能过桥,桥的可以状态标志为不可以用
for( int i=1 ;i<=12;i++)
{
Thread t = new Thread(new Person(true,i,b));
t.start();
}
//桥的可用状态给左端第10个人,可以自定义给谁
b.state = true;
Thread t = new Thread(new Person(false, 10, b));
}

}

class Person implements Runnable {

public Bridge bridge;//桥
private String hand;//在桥哪一端
int No;//序号

public Person(boolean side, int i, Bridge bridge) {

this.No = i;
this.bridge = bridge;
if(bridge.state)
{
System.out.println(i+"已经过桥。");
}
if (side) {
this.hand = new String("右");
} else {
this.hand = new String("左");
}
}

//过桥方法
public synchronized void through() throws InterruptedException {

if (bridge.state) {
System.out.println(hand+"边第"+No + "在过桥");
bridge.open( No);
} else {
bridge.lock(No);

}
}

public void run() {
try {
Thread.sleep(1000);
// System.out.println(No+hand+" 边已经过桥!");
through();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class Bridge {
//可用状态判断true:可用
public boolean state = false;
//自行实例化
public static Bridge getInstance() {
return new Bridge();
}

public synchronized void open(int i) throws InterruptedException {
if (state) {
Thread.sleep(1000);
this.state=true;
notify();
}
}

public synchronized void lock(int i) throws InterruptedException {
if (!state) {
this.state=false;
System.out.println(i + " 在等待.");
wait();
}
}
}

4. Java 程序中的多线程源代码

你的问题倒让人看不懂。。。首先这里是 C#/.net板块,居然问了个java的问题。。。其次,内容看不懂?什么内容看不懂?加代码又是什么?java程序中的多线程原码是指什么原码。。。

5. 如何用Java编写多线程

在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。
对于直接继承Thread的类来说,代码大致框架是:
?
123456789101112 class 类名 extends Thread{ 方法1; 方法2; … public void run(){ // other code… } 属性1; 属性2; … }
先看一个简单的例子:
?
/** * @author Rollen-Holt 继承Thread类,直接调用run方法 * */class hello extends Thread { public hello() { } public hello(String name) { this.name = name; } public void run() { for (int i = 0; i < 5; i++) { System.out.println(name + "运行 " + i); } } public static void main(String[] args) { hello h1=new hello("A"); hello h2=new hello("B"); h1.run(); h2.run(); } private String name; }
【运行结果】:
A运行 0
A运行 1
A运行 2
A运行 3
A运行 4
B运行 0
B运行 1
B运行 2
B运行 3
B运行 4
我们会发现这些都是顺序执行的,说明我们的调用方法不对,应该调用的是start()方法。
当我们把上面的主函数修改为如下所示的时候:
?
123456 public static void main(String[] args) { hello h1=new hello("A"); hello h2=new hello("B"); h1.start(); h2.start(); }
然后运行程序,输出的可能的结果如下:
A运行 0
B运行 0
B运行 1
B运行 2
B运行 3
B运行 4
A运行 1
A运行 2
A运行 3
A运行 4
因为需要用到CPU的资源,所以每次的运行结果基本是都不一样的,呵呵。
注意:虽然我们在这里调用的是start()方法,但是实际上调用的还是run()方法的主体。
那么:为什么我们不能直接调用run()方法呢?
我的理解是:线程的运行需要本地操作系统的支持。
如果你查看start的源代码的时候,会发现:
?
1234567891011121314151617 public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0 || this != me) throw new IllegalThreadStateException(); group.add(this); start0(); if (stopBeforeStart) { stop0(throwableFromStop); } } private native void start0();
注意我用红色加粗的那一条语句,说明此处调用的是start0()。并且这个这个方法用了native关键字,次关键字表示调用本地操作系统的函数。因为多线程的实现需要本地操作系统的支持。
但是start方法重复调用的话,会出现java.lang.IllegalThreadStateException异常。
通过实现Runnable接口:

大致框架是:
?
123456789101112 class 类名 implements Runnable{ 方法1; 方法2; … public void run(){ // other code… } 属性1; 属性2; … }

来先看一个小例子吧:
?
2930 /** * @author Rollen-Holt 实现Runnable接口 * */class hello implements Runnable { public hello() { } public hello(String name) { this.name = name; } public void run() { for (int i = 0; i < 5; i++) { System.out.println(name + "运行 " + i); } } public static void main(String[] args) { hello h1=new hello("线程A"); Thread demo= new Thread(h1); hello h2=new hello("线程B"); Thread demo1=new Thread(h2); demo.start(); demo1.start(); } private String name; }
【可能的运行结果】:
线程A运行 0
线程B运行 0
线程B运行 1
线程B运行 2
线程B运行 3
线程B运行 4
线程A运行 1
线程A运行 2
线程A运行 3
线程A运行 4

关于选择继承Thread还是实现Runnable接口?
其实Thread也是实现Runnable接口的:
?
12345678 class Thread implements Runnable { //… public void run() { if (target != null) { target.run(); } } }
其实Thread中的run方法调用的是Runnable接口的run方法。不知道大家发现没有,Thread和Runnable都实现了run方法,这种操作模式其实就是代理模式。关于代理模式,我曾经写过一个小例子呵呵,大家有兴趣的话可以看一下:http://www.cnblogs.com/rollenholt/archive/2011/08/18/2144847.html
Thread和Runnable的区别:
如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。
?
/** * @author Rollen-Holt 继承Thread类,不能资源共享 * */class hello extends Thread { public void run() { for (int i = 0; i < 7; i++) { if (count > 0) { System.out.println("count= " + count--); } } } public static void main(String[] args) { hello h1 = new hello(); hello h2 = new hello(); hello h3 = new hello(); h1.start(); h2.start(); h3.start(); } private int count = 5; }

【运行结果】:
count= 5
count= 4
count= 3
count= 2
count= 1
count= 5
count= 4
count= 3
count= 2
count= 1
count= 5
count= 4
count= 3
count= 2
count= 1
大家可以想象,如果这个是一个买票系统的话,如果count表示的是车票的数量的话,说明并没有实现资源的共享。
我们换为Runnable接口:
?
12345678910111213141516171819 /** * @author Rollen-Holt 继承Thread类,不能资源共享 * */class hello implements Runnable { public void run() { for (int i = 0; i < 7; i++) { if (count > 0) { System.out.println("count= " + count--); } } } public static void main(String[] args) { hello he=new hello(); new Thread(he).start(); } private int count = 5; }

【运行结果】:
count= 5
count= 4
count= 3
count= 2
count= 1

总结一下吧:
实现Runnable接口比继承Thread类所具有的优势:
1):适合多个相同的程序代码的线程去处理同一个资源
2):可以避免java中的单继承的限制
3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。

所以,本人建议大家劲量实现接口。
?

6. java多线程编译,求代码!

public class Main {

private static ArrayBlockingQueue<String> file_q = new ArrayBlockingQueue<String>(1000);
private static ArrayBlockingQueue<String> console_q = new ArrayBlockingQueue<String>(1000);

public static void main(String[] args) {
new Thread(new Thread_A()).start();
new Thread(new Thread_B()).start();
new Thread(new Thread_C()).start();
}

static class Thread_A implements Runnable {

public void run() {
BufferedReader thread_a = new BufferedReader(new InputStreamReader(System.in));
String inputString;
while (true) {
try {
inputString = thread_a.readLine();
char firstChar = inputString.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
file_q.put(inputString);//大写,放入写文件队列
} else {
console_q.put(inputString);//非大写,放入写控制台队列
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

/**
* 文件输出线程
*/
static class Thread_B implements Runnable {
public void run() {
while (true) {
try {
String to_write_file_str = file_q.poll(300L, TimeUnit.MILLISECONDS);
if (null != to_write_file_str && to_write_file_str.trim().length() > 0) {
RandomAccessFile randomFile = new RandomAccessFile("E:/test.txt", "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(String.format("%s\r\n", to_write_file_str));
randomFile.close();
System.out.println("Thread B writed!");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 控制台输出线程
*/
static class Thread_C implements Runnable {
public void run() {
while (true) {
try {
String to_write_console_str = console_q.poll(300L, TimeUnit.MILLISECONDS);
if (null != to_write_console_str && to_write_console_str.trim().length() > 0)
System.out.printf("Thread C say: %s%n", to_write_console_str);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}

7. java多线程编程代码如下,输出结果如下:

首先,你同步的是具体的某个Test实例, 对于那个实例来说,实际上只有一个线程访问了那个代码块,但是sum和other却是多个线程同时去进行访问,实际上这是不安全的,如果你想实现每次都输出10000的效果,那么正确的应该是在Test.class上加锁,而不是获取Test实例的锁,修改后的代码如下:

publicclassTestextendsThread{

publicstaticintsum=10000;

publicstaticintother=0;

publicvoidgetMoney(){
synchronized(Test.class){

System.out.println(Thread.currentThread().getName()+"开始执行");
sum=sum-100;

System.out.println("sum-100");

other=other+100;

System.out.println("other+100");

System.out.println(sum+other);
System.out.println(Thread.currentThread().getName()+"执行完成");
}

}

publicvoidrun(){

getMoney();

}

publicstaticvoidmain(String[]agrs){

Threadt[]=newThread[10];

for(inti=0;i<=9;i++){

t[i]=newTest();

t[i].start();

}

}

}

// 上面代码能得到你的结果


8. 简单Java多线程代码求解

因为你new了两次
试着在Task类内创建一个对象 然后锁住这个对象

9. 求一个JAVA多线程例子,最好有代码,谢谢啦!

package a.b.test;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Calculate1000 implements Callable<Integer>{
public Calculate1000(){}

public Calculate1000(int a, int b){
this.a = a;
this.b = b;

}

int a;
int b;

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//同步
Calculate1000 ca1 = new Calculate1000();
Date ds1 = new Date();
int result = 0;
for(int i = 1 ; i <= 1000 ; i++){
result = ca1.add(i, result);
}
System.out.println(result);
System.out.println("同步用时" + (new Date().getTime() - ds1.getTime()) + "MS");

//异步
Date ds2 = new Date();
result = 0;
ExecutorService es = Executors.newFixedThreadPool(2);
Future<Integer> future1 = es.submit(new Calculate1000(1,500));
Future<Integer> future2 = es.submit(new Calculate1000(501,1000));
result = future1.get() + future2.get();
System.out.println(result);
System.out.println("异步用时" + (new Date().getTime() - ds2.getTime()) + "MS");
es.shutdown();
}
private int add(int a, int b) throws Exception{
Thread.sleep(10);
return a + b;
}
@Override
public Integer call() throws Exception {
int res = 0;
for(int i = a ; i <= b ; i++){
res = this.add(res, i);
}
return res;
}
}
楼主你试一下这段代码行不行,行的话请采纳!

10. 求java多线程遍历目录的完整代码,能运行的那种

目录结构为树型结构,用多线程不大好做,线程最多在前几层进行分割,比如每个目录下有两个目录,共5层,那么root目录下就能启用2个线程分别进行遍历,所以第二层就启动了2个线程进行遍历,加上主线程共三个线程,虽然这样做是可以做,但是要更具实际情况进行线程的规划,否则容易线程过多导致cpu超负荷,或者假死,再提一点,遍历目录不建议用递归来写,因为目录较多容易栈溢出。
随手写了个,会有点bug就是关闭线程池的时候,还有就是有可能目录太多进入拒绝策略,这个东西 可以考虑使用令牌桶算法,或者计数器算法来做。这里提供个简单的例子。
public class TraverseUtil {
public static BlockingQueue blockingQueue = new LinkedBlockingQueue(100);
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100,100,10, TimeUnit.SECONDS,blockingQueue);
public static void traverseFolder2(String path) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
System.out.println("文件夹是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath());
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
traverseFolder2(file2.getAbsolutePath());
}
});
} else {
System.out.println("文件:" + file2.getAbsolutePath());
}
}
}
} else {
System.out.println("文件不存在!");
}
}
public static void main(String[] args) throws InterruptedException {
traverseFolder2("C:\\Users\\a8932\\Desktop\\md");
}
}

阅读全文

与java多线程源码相关的资料

热点内容
安卓qq邮箱格式怎么写 浏览:429
如何电信租用服务器吗 浏览:188
编程中计算根号的思维 浏览:181
可爱的程序员16集背景音乐 浏览:446
软件代码内容转换加密 浏览:795
什么app看电视不要钱的 浏览:16
乌班图怎么安装c语言编译器 浏览:278
plc通讯块编程 浏览:923
我的世界服务器怎么清地皮 浏览:421
ftp服务器如何批量改名 浏览:314
网易我的世界服务器成员如何传送 浏览:268
公司云服务器远程访问 浏览:633
法哲学pdf 浏览:637
清大阅读app是什么 浏览:447
怎么用qq浏览器整体解压文件 浏览:585
肺组织压缩15 浏览:270
安卓手机为什么换电话卡没反应 浏览:797
诸子集成pdf 浏览:339
php注册框代码 浏览:718
手机加密好还是不加好好 浏览:815