设计两个线程一个线程做加运算一个线程减运算

package thread;


public class Testthread {


/**
* @param args
* 设计两个线程一个线程做加运算一个线程减运算
* 知识点:1、线程 实现Runnable接口 继承Thread类
* 2、线程的启动start()
* 3、线程安全 同步方法和同步代码块 synchronized
* 4、内部类的创建 Des des=tt.new Des(); 
*/
int j=0;
static Testthread tt=new Testthread();
static Increase inc=tt.new Increase();
static Des des=tt.new Des();
static Thread th1=new Thread(inc);
static Thread th2=new Thread(des);
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;



th1.start();
// th1.start();

th2.start();
// th2.start();

         
}

private synchronized void  add(){
for(int i=0;i<100;i++){
j++;
System.out.println(Thread.currentThread().getName()+"加运算"+j);
}}

private synchronized void min(){
for(int i=0;i<100;i++){
j--;
System.out.println(Thread.currentThread().getName()+"减运算"+j);
}
}
class Increase implements Runnable{


@Override
public void run() {

add();
}

}

class Des implements Runnable{


@Override
public void run() {
// TODO Auto-generated method stub
min();
}


}


}
以下是使用Java编写多线程稀疏矩阵乘法运算函数的示例代码: ```java public class SparseMatrixMultiplication { private static final int NUM_THREADS = 4; public static int[][] multiply(int[][] A, int[][] B) { int m = A.length; int n = B[0].length; int[][] result = new int[m][n]; List<Thread> threads = new ArrayList<>(); // Divide the rows of A into NUM_THREADS parts and create a thread for each part int chunkSize = m / NUM_THREADS; for (int i = 0; i < NUM_THREADS; i++) { int startRow = i * chunkSize; int endRow = i == NUM_THREADS - 1 ? m : (i + 1) * chunkSize; Thread thread = new Thread(new MultiplicationTask(A, B, result, startRow, endRow)); threads.add(thread); thread.start(); } // Wait for all threads to finish for (Thread thread : threads) { try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } return result; } private static class MultiplicationTask implements Runnable { private int[][] A; private int[][] B; private int[][] result; private int startRow; private int endRow; public MultiplicationTask(int[][] A, int[][] B, int[][] result, int startRow, int endRow) { this.A = A; this.B = B; this.result = result; this.startRow = startRow; this.endRow = endRow; } @Override public void run() { int n = A[0].length; int p = B.length; for (int i = startRow; i < endRow; i++) { for (int k = 0; k < n; k++) { if (A[i][k] != 0) { for (int j = 0; j < p; j++) { if (B[k][j] != 0) { result[i][j] += A[i][k] * B[k][j]; } } } } } } } } ``` 在此示例代码中,我们定义了一个 `SparseMatrixMultiplication` 类,其中包含一个名为 `multiply` 的函数,它接受两个稀疏矩阵 `A` 和 `B` 作为输入,并返回它们的乘积矩阵。 在函数中,我们首先创建一个大小为 `NUM_THREADS` 的线程列表,并将矩阵 `A` 的行分成 `NUM_THREADS` 个部分,为每个部分创建一个线程。每个线程都执行名为 `MultiplicationTask` 的任务,该任务将计算矩阵 `A` 的一部分与矩阵 `B` 相乘,并将结果存储在乘积矩阵 `result` 中。 `MultiplicationTask` 任务实现了 `Runnable` 接口,它接受矩阵 `A`、矩阵 `B`、乘积矩阵 `result`、开始行和结束行作为输入,并计算 `A` 的一部分与 `B` 的乘积并将结果存储在 `result` 中。 在任务中,我们首先获取矩阵 `A` 的列数 `n` 和矩阵 `B` 的行数 `p`,然后在每个行 `i` 和列 `j` 上循环乘以 `A[i][k]` 和 `B[k][j]`,其中 `k` 表示矩阵 `A` 和矩阵 `B` 的交叉点。注意,我们只计算非零元素,因为稀疏矩阵中大多数元素都是零,因此计算零元素是浪费时间和资源的。 最后,我们等待所有线程完成,并返回乘积矩阵 `result`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值