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

多线程编程代码

发布时间:2022-05-03 18:15:37

‘壹’ java 多线程编程

可以同时运行。另外,你的类名最好是大写;想要使用线程类,最好是实现Runnable接口。

‘贰’ VB怎么实现稳定的多线程程序

相关代码如下:
Private
Const
MyEvent
As
String
=
"m5home"
Dim
hEvent
As
Long
Sub
Main()
If
GetEventHandle
=
0
Then
'由于每个新对象的建立,都要执行Main()过程,因此使用事件对象来进行判断.
hEvent
=
CreateEvent(0&,
False,
False,
MyEvent)
'在单元线程下,模块里的全局变量已经没有用了.frmMain.Show
End
If
End
Sub
Private
Function
GetEventHandle()
As
Long
GetEventHandle
=
OpenEvent(EVENT_ALL_ACCESS,
False,
MyEvent)
Call
CloseHandle(GetEventHandle)
End
Function
Public
Function
Quit()
Call
CloseHandle(hEvent)
End
Function
Private
Const
MyEvent
As
String
=
"m5home"
Dim
hEvent
As
Long
Sub
Main()
If
GetEventHandle
=
0
Then
'由于每个新对象的建立,都要执行Main()过程,因此使用事件对象来进行判断.
hEvent
=
CreateEvent(0&,
False,
False,
MyEvent)
'在单元线程下,模块里的全局变量已经没有用了.frmMain.Show
End
If
End
Sub
Private
Function
GetEventHandle()
As
Long
GetEventHandle
=
OpenEvent(EVENT_ALL_ACCESS,
False,
MyEvent)
Call
CloseHandle(GetEventHandle)
End
Function
Public
Function
Quit()
Call
CloseHandle(hEvent)
End
Function
由于事件对象是系统范围的,因此可以比较完美的完成这个工作.
同时事件对象在进程消失后,会自动释放,也方便:)
示例代码可以生成一个新的单元线程,并在这个线程里面显示一个窗体,窗体进行一个大循环.
循环内没有放入DoEvents函数,因此会造成循环所在窗体无响应.
而此时主窗体不受影响.

‘叁’ 1. 写出用Java编写多线程程序的两种常用方法

1、继承Thread,然后生成对象

2、用类A实现runable接口,然后用你实现runnable的类A,生成Thread对象 Thread(A对象);

API 上说明如下:

创建新执行线程有两种方法。一种方法是将类声明为 Thread 的子类。该子类应重写 Thread 类的
run 方法。接下来可以分配并启动该子类的实例。例如,计算大于某一规定值的质数的线程可以写成:

classPrimeThreadextendsThread{
longminPrime;
PrimeThread(longminPrime){
this.minPrime=minPrime;
}

publicvoidrun(){
//
...
}
}

然后,下列代码会创建并启动一个线程:

PrimeThreadp=newPrimeThread(143);
p.start();

创建线程的另一种方法是声明实现 Runnable 接口的类。该类然后实现 run
方法。然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动。采用这种风格的同一个例子如下所示:

implementsRunnable{
longminPrime;
PrimeRun(longminPrime){
this.minPrime=minPrime;
}

publicvoidrun(){
//
...
}
}

然后,下列代码会创建并启动一个线程:

rimeRunp=newPrimeRun(143);

newThread(p).start();

‘肆’ 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();

}

}

}

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


‘伍’ 什么是Java多线程编程

一、 什么是多线程:

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

‘陆’ c# 多线程编程

稍作修改,运行代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TestFormsApp
{
public partial class Form1 : Form
{
Serial serial = new Serial();//自定义类Serial
CheckIsOver ck = new CheckIsOver();//自定义CheckIsOver

public Form1()
{
InitializeComponent();

serial.Starting = true;
Thread derThread = new Thread(serial.OpenSerial);
derThread.Start();

ck.Starting = true;
Thread derThread1 = new Thread(ck.main1);
derThread1.Start();

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 线程退出
serial.Starting = false;
ck.Starting = false;
}
}
}

在输出窗口中可以看到线程运行情况:
Serial's thread:0
CheckIsOver's thread:0
CheckIsOver's thread:1
Serial's thread:1
Serial's thread:2
CheckIsOver's thread:2
Serial's thread:3
CheckIsOver's thread:3
线程 0xe48 已退出,返回值为 0 (0x0)。
线程 0x380 已退出,返回值为 0 (0x0)。

楼主有邮箱发给我,我把刚做的例子发给你

‘柒’ 求一个Java多线程程序代码的注释

多线程实现方式有两种:继承Thread类和实现Runnable接口,二者区别及示例如下:
1.实现方式不同及可扩展性

Thread 采用单继承的方式,继承Thread的类不可再继承其它类。
Runnable接口采用实现接口的方式,可再继承其它类。

2.方法执行体中 run( ),调用getName方法不同。

Thread可直接通过成员方法getName()获取当前运行时的线程名称。
Runnable中 必须通过 Thread.currentThread().getName()方法得到。

3.启动线程为就绪状态的方法不同。

Thread类 通过 new ThreadTest().start()的方法可以同时启动多个线程 其线程名默认为 Thread-0/1/2。
Runnable 需要通过 RunableTest rt= new RunableTest();
new Thread(rt,"线程0" ).start();
new Thread(rt,"线程1" ).start();
4.是否共享内存资源。

Thread 多个线程之间不共享内存资源 各自独立
Runnable 多个线程共享同一个内存资源。

方式一:
public class ThreadTest extends Thread {

private int i ;

public void run(){

for(;i <100;i++){

System. out.println("run:" +getName()+" "+ i);

}
}
public static void main(String[] args) {

for(int i=0;i<100;i++){

System.out.println("main:" +Thread.currentThread ().getName()+" " +i);
if(i==20){

new ThreadTest().start();
new ThreadTest().start();

}
}
}
}
方式二
public class RunableTest implements Runnable{
private int i ;
@Override

public void run(){

for(;i <100;i++){

String name=Thread. currentThread().getName();
System. out.println("run:" +name+" "+ i);

}
}
public static void main(String[] args) throws InterruptedException {

for(int i=0;i<100;i++){

System. out.println(Thread.currentThread().getName()+ " "+i);

if(i==20){

RunableTest rt= new RunableTest();

new Thread(rt,"线程0" ).start();

new Thread(rt,"线程1" ).start();

}

}

}
}

‘捌’ 求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();
}
}
}

‘玖’ 求java多线程编程代码,要有注释,非诚勿扰~~~做出后分不是问题~~

这个是实验时候做的例子 希望对你有用 包括实现多线程的两种方法
一、用创建Thread 类的子类的方法实现多线程一
源程序:
class FruitThread extends Thread{
public FruitThread(String str){
super(str);
}
public void run(){ //线程的run方法
for(int i=0;i<5;i++){
System.out.println(i+" "+getName());
try{
sleep((int)(Math.random()*1000));//等待
}
catch(InterruptedException e){
}
}
}
}
public class TwoFtuit{
public static void main(String[] a){
new FruitThread("苹果").start(); //第一个线程
new FruitThread("梨").start(); //第二个线程
}
}
实验结果:
E:\>javac TwoFtuit.java

E:\>java TwoFtuit
0 苹果
0 梨
1 苹果
2 苹果
1 梨
3 苹果
2 梨
4 苹果
3 梨
4 梨

二、用实现Runnable 接口的方法实现多线程
源程序:
class outputClass implements Runnable{
String name;
outputClass(String s){
name =s;
}
public void run(){
for(int i=0;i<3;i++){
System.out.println(name);
Thread.yield();
}
}
}
public class runThreads{
public static void main(String[] a){
outputClass out1=new outputClass("Thread1");
outputClass out2=new outputClass("Thread2");
Thread t1=new Thread(out1);
Thread t2=new Thread(out2);
t1.start();
t2.start();
}
}
实验结果:
E:\java>javac runThreads.java

E:\java>java runThreads
Thread1
Thread2
Thread1
Thread2
Thread1
Thread2

‘拾’ Linux多线程编程

程序代码test.c共两个线程,一个主线程,一个读缓存区的线程:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char globe_buffer[100];

void *read_buffer_thread(void *arg); //这里先声明一下读缓存的线程,具体实现写在后面了

int main()
{
int res,i;
pthread_t read_thread;
for(i=0;i<20;i++)
globe_buffer[i]=i;
printf("\nTest thread : write buffer finish\n");
sleep(3);\\这里的3秒是多余,可以不要。
res = pthread_create(&read_thread, NULL, read_buffer_thread, NULL);
if (res != 0)
{
printf("Read Thread creat Error!");
exit(0);
}
sleep(1);
printf("waiting for read thread to finish...\n");

res = pthread_join(read_thread, NULL);
if (res != 0)
{
printf("read thread join failed!\n");
exit(0);
}
printf("read thread test OK, have fun!! exit ByeBye\n");
return 0;
}
void *read_buffer_thread(void *arg)
{
int i,x;
printf("Read buffer thread read data : \n");
for(i=0;i<20;i++)
{
x=globe_buffer[i];
printf("%d ",x);
globe_buffer[i]=0;//清空
}
printf("\nread over\n");
}
---------------------------------------------------------------------------------
以上程序编译
gcc -D_REENTRANT test.c -o test.o –lpthread
运行这个程序:
$ ./test.o:

阅读全文

与多线程编程代码相关的资料

热点内容
fibonacci数列算法 浏览:775
产品经理要和程序员吵架吗 浏览:252
grub2命令行 浏览:618
无法获取加密卡信息 浏览:774
云服务器网卡充值 浏览:509
编程就是软件 浏览:49
服务器如何添加权限 浏览:437
引用指针编程 浏览:851
手机加密日记本苹果版下载 浏览:63
命令行括号 浏览:176
java程序升级 浏览:490
排序算法之插入类 浏览:227
gcccreate命令 浏览:73
海尔监控用什么app 浏览:64
系统盘被压缩开不了机 浏览:984
linuxredis30 浏览:541
狸窝pdf转换器 浏览:696
ajax调用java后台 浏览:906
活塞式压缩机常见故障 浏览:614
break算法 浏览:731