多线程 线程池 最大线程数
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 ThreadTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1);
ThreadPool.QueueUserWorkItem(DoWork, i);
}
}
private void DoWork(object o)
{
Thread.Sleep(1000);
int id=Thread.CurrentThread.ManagedThreadId;
int userThread, ioThread;
ThreadPool.GetAvailableThreads(out userThread,out ioThread);
int userMaxThread, ioMaxThread;
ThreadPool.GetMaxThreads(out userMaxThread, out ioMaxThread);
this.Invoke(new Action(() => {
textBox1.Text = string.Format("{0}111i:{1} ID:{2} HashCode:{3} Aviad:{4} IO:{5}\r\n", textBox1.Text,
o.ToString(), id,
Thread.CurrentThread.GetHashCode(), userThread, userMaxThread);
textBox1.SelectionStart = textBox1.TextLength;
textBox1.ScrollToCaret();
}));
for (int k = 0; k < 10; k++)
{
ThreadPool.SetMaxThreads(10, 1);
ThreadPool.QueueUserWorkItem(DoWork2, k);
}
}
private void DoWork2(object o)
{
Thread.Sleep(100);
int id = Thread.CurrentThread.ManagedThreadId;
int userThread, ioThread;
ThreadPool.GetAvailableThreads(out userThread, out ioThread);
int userMaxThread, ioMaxThread;
ThreadPool.GetMaxThreads(out userMaxThread, out ioMaxThread);
this.Invoke(new Action(() =>
{
textBox1.Text = string.Format("{0}222i:{1} ID:{2} HashCode:{3} Aviad:{4} IO:{5}\r\n", textBox1.Text,
o.ToString(), id,
Thread.CurrentThread.GetHashCode(), userThread, userMaxThread);
textBox1.SelectionStart = textBox1.TextLength;
textBox1.ScrollToCaret();
}));
}
private void Form1_Load(object sender, EventArgs e)
{
ThreadPool.SetMaxThreads(10, 1);
}
}
}

在线程中启用线程,最大线程数无论怎么设置都无效果,都是1023,这是为什么哦,怎么处理?
...全文
相关推荐
多线程简单应用-线程池
线程的一些简单应用
1.Java分布式与微服务实战\多线程与分布式\第1节 线程池
第1节 线程池
线程池之ThreadPoolExecutor.docx
线程池的工作主要是控制运行的线程的数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量,那么超出数量的线程排队等候,等其他线程执行完毕再从队列中取出任务来执行。 在开发过程中,合理地使用线程池能够带来3个好处: 降低资源消耗。 通过重复利用已创建的线程降低线程创建和销毁造成的消耗; 提高响应速度。 当任务到达时,任务可以不需要等到线程创建就能立即执行; 提高线程的可管理性。 线程是稀缺资源,如果无限制地创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一分配、调优和监控。
线程池java写的代码
线程池java写的代码
Spring boot使用多线程过程步骤解析
主要介绍了Spring boot使用多线程过程步骤解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下


