小程序发布 服务器端口,小程序服务器不是443端口怎么办

多个线程同时对一个变量进行++操作是不是安全

总值不论是否采取同步,都是不会变的,但是实际应用中常常不只求总值,还有对全局变量的判断和使用,比如第一个线程当i++循环两次时,输出i=2。虽然第一个线程代码中确实循环两次,i从0加到2;但在没有同步的情况下另一线程也在对i加1,所以原本输出i=2的地方可能输出i=3,4....从而造成不一致性。

以下是加lock代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SqlClient;

using System.Threading;

using System.Runtime.CompilerServices;

using System.Data;

public class LockMe

{

}

public class pp

{

private int i = 0;

private LockMe l=new LockMe();

public void plus1()

{

lock(this.l)

{

for (int j = 0; j < 3; j++)

i++;

}

}

public void plus2()

{

lock (this.l)

{

for (int j = 0; j < 3; j++)

i++;

}

}

}

public class PP

{

static void Main()

{

pp p = new pp();

ThreadStart t1 = new ThreadStart(p.plus1);

ThreadStart t2 = new ThreadStart(p.plus2);

Thread T1 = new Thread(t1);

Thread T2 = new Thread(t2);

T1.Start();

T2.Start();

Console.WriteLine(p.i);

}

}

收起

6f93e2c92fc5e58e2ec18df1db0b9f08.png

阅读全文

9ded402a52cdd2b9481e864a5093252c.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个示例代码,实现了通过微信小程序连接mqtt服务器的功能: ```javascript // 引入mqtt库 const mqtt = require('../../utils/mqtt.min.js') Page({ data: { client: null, topic: '', message: '' }, // 连接mqtt服务器 connectMqtt: function() { const client = mqtt.connect('wxs://your-mqtt-server.com:443', { // 你的mqtt用户名 username: 'your-username', // 你的mqtt密码 password: 'your-password', // 客户端ID,每个客户端应该有唯一的ID clientId: 'your-client-id' }) // 监听连接事件 client.on('connect', function () { console.log('connected') // 订阅主题 client.subscribe('your-topic', function (err) { if (!err) { console.log('subscribed') } }) }) // 监听接收消息事件 client.on('message', function (topic, message) { console.log(topic, message.toString()) }) this.setData({ client: client, topic: '', message: '' }) }, // 发送消息 publishMessage: function() { const { client, topic, message } = this.data if (client && topic && message) { client.publish(topic, message) } }, // 输入主题 inputTopic: function(e) { this.setData({ topic: e.detail.value }) }, // 输入消息 inputMessage: function(e) { this.setData({ message: e.detail.value }) }, // 断开连接 disconnectMqtt: function() { const { client } = this.data if (client) { client.end() this.setData({ client: null }) } } }) ``` 需要注意的是,由于微信小程序限制了网络请求的协议,只支持 `http` 和 `https` 协议,因此需要使用 `wxs` 协议来连接 `mqtt` 服务器。同时,由于 `mqtt` 是一个基于 `TCP` 协议的协议,因此需要使用 `443` 端口来连接服务器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值